linux-trace-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: linux-trace-devel@vger.kernel.org
Cc: Sameeruddin shaik <sameeruddin.shaik8@gmail.com>
Subject: [PATCH 08/13 v2] libtracefs: Allow for setting filters with regex expressions
Date: Mon, 29 Mar 2021 20:51:31 -0400	[thread overview]
Message-ID: <20210330005248.391852830@goodmis.org> (raw)
In-Reply-To: 20210330005123.151740983@goodmis.org

From: "Steven Rostedt (VMware)" <rostedt@goodmis.org>

All for full "regex(3)" processing of setting functions in the
set_ftrace_filter file. Check if the filter passed in is just a glob
expression that the kernel can process, or if it is a regex that should look
at the available_filter_functions list instead.

If it is a regex, it will read the available_filter_functions and write in
each function as it finds it.

Link: https://lore.kernel.org/linux-trace-devel/20210323013225.451281989@goodmis.org

Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
---
 Documentation/libtracefs-function-filter.txt |  10 ++
 src/tracefs-tools.c                          | 139 ++++++++++++++++---
 2 files changed, 128 insertions(+), 21 deletions(-)

diff --git a/Documentation/libtracefs-function-filter.txt b/Documentation/libtracefs-function-filter.txt
index c0c89f372c21..88aa3b923d54 100644
--- a/Documentation/libtracefs-function-filter.txt
+++ b/Documentation/libtracefs-function-filter.txt
@@ -32,6 +32,16 @@ _errs_, is a pointer to an array of strings, which will be allocated if
 any of filters fail to match any available function, If _errs_ is NULL, it will
 be ignored.
 
+A filter in the array of _filters_ may be either a straight match of a
+function, a glob or regex(3). a glob is where '*' matches zero or more
+characters, '?' will match zero or one character, and '.' only matches a
+period. If the filter is determined to be a regex (where it contains
+anything other than alpha numeric characters, or '.', '*', '?') the filter
+will be processed as a regex(3) following the rules of regex(3), and '.' is
+not a period, but will match any one character. To force a regular
+expression, either prefix the filter with a '^' or append it with a '$' as
+all filters will act as complete matches of functions anyway.
+
 returns 0  on success, 1 or -x (where x is an integer) on error.
 
 RETURN VALUE
diff --git a/src/tracefs-tools.c b/src/tracefs-tools.c
index 470502b07f7d..d1a448459c6f 100644
--- a/src/tracefs-tools.c
+++ b/src/tracefs-tools.c
@@ -18,8 +18,9 @@
 #include "tracefs.h"
 #include "tracefs-local.h"
 
-#define TRACE_CTRL	"tracing_on"
-#define TRACE_FILTER	"set_ftrace_filter"
+#define TRACE_CTRL		"tracing_on"
+#define TRACE_FILTER		"set_ftrace_filter"
+#define TRACE_FILTER_LIST	"available_filter_functions"
 
 static const char * const options_map[] = {
 	"unknown",
@@ -421,8 +422,53 @@ struct func_filter {
 	const char		*filter;
 	regex_t			re;
 	bool			set;
+	bool			is_regex;
 };
 
+static bool is_regex(const char *str)
+{
+	int i;
+
+	for (i = 0; str[i]; i++) {
+		switch (str[i]) {
+		case 'a' ... 'z':
+		case 'A'...'Z':
+		case '_':
+		case '0'...'9':
+		case '*':
+		case '.':
+			/* Dots can be part of a function name */
+		case '?':
+			continue;
+		default:
+			return true;
+		}
+	}
+	return false;
+}
+
+static char *update_regex(const char *reg)
+{
+	int len = strlen(reg);
+	char *str;
+
+	if (reg[0] == '^' && reg[len - 1] == '$')
+		return strdup(reg);
+
+	str = malloc(len + 3);
+	if (reg[0] == '^') {
+		strcpy(str, reg);
+	} else {
+		str[0] = '^';
+		strcpy(str + 1, reg);
+		len++; /* add ^ */
+	}
+	if (str[len - 1] != '$')
+		str[len++]= '$';
+	str[len] = '\0';
+	return str;
+}
+
 /*
  * Convert a glob into a regular expression.
  */
@@ -488,8 +534,13 @@ static int write_filter(int fd, const char *filter, const char *module)
 	return 0;
 }
 
-static int check_available_filters(struct func_filter *func_filters,
-				   const char *module, const char ***errs)
+enum match_type {
+	FILTER_CHECK,
+	FILTER_WRITE,
+};
+
+static int match_filters(int fd, struct func_filter *func_filters,
+			 const char *module, enum match_type type)
 {
 	char *line = NULL;
 	size_t size = 0;
@@ -499,7 +550,7 @@ static int check_available_filters(struct func_filter *func_filters,
 	int mlen;
 	int i;
 
-	path = tracefs_get_tracing_file("available_filter_functions");
+	path = tracefs_get_tracing_file(TRACE_FILTER_LIST);
 	if (!path)
 		return 1;
 
@@ -530,39 +581,76 @@ static int check_available_filters(struct func_filter *func_filters,
 			    (mtok[mlen + 1] != ']'))
 				goto next;
 		}
-		for (i = 0; func_filters[i].filter; i++) {
-			if (match(tok, &func_filters[i]))
-				func_filters[i].set = true;
+		switch (type) {
+		case FILTER_CHECK:
+			/* Check, checks a list of filters */
+			for (i = 0; func_filters[i].filter; i++) {
+				if (match(tok, &func_filters[i]))
+					func_filters[i].set = true;
+			}
+			break;
+		case FILTER_WRITE:
+			/* Writes only have one filter */
+			if (match(tok, func_filters)) {
+				ret = write_filter(fd, tok, module);
+				if (ret)
+					goto out;
+			}
+			break;
 		}
 	next:
 		free(line);
 		line = NULL;
 		len = 0;
 	}
+ out:
+	free(line);
 	fclose(fp);
 
+	return ret;
+}
+
+static int check_available_filters(struct func_filter *func_filters,
+				   const char *module, const char ***errs)
+{
+	int ret;
+	int i;
+
+	ret = match_filters(-1, func_filters, module, FILTER_CHECK);
+	/* Return here if success or non filter error */
+	if (ret >= 0)
+		return ret;
+
+	/* Failed on filter, set the errors */
 	ret = 0;
 	for (i = 0; func_filters[i].filter; i++) {
 		if (!func_filters[i].set)
 			add_errors(errs, func_filters[i].filter, ret--);
 	}
-
 	return ret;
 }
 
-static int controlled_write(int fd, const char **filters,
+static int set_regex_filter(int fd, struct func_filter *func_filter,
+			    const char *module)
+{
+	return match_filters(fd, func_filter, module, FILTER_WRITE);
+}
+
+static int controlled_write(int fd, struct func_filter *func_filters,
 			    const char *module, const char ***errs)
 {
 	int ret = 0;
 	int i;
 
-	for (i = 0; filters[i]; i++) {
+	for (i = 0; func_filters[i].filter; i++) {
+		const char *filter = func_filters[i].filter;
 		int r;
 
-		r = write_filter(fd, filters[i], module);
-		if (r < 0) {
-			add_errors(errs, filters[i], ret--);
-		} else if (r > 0) {
+		if (func_filters[i].is_regex)
+			r = set_regex_filter(fd, &func_filters[i], module);
+		else
+			r = write_filter(fd, filter, module);
+		if (r > 0) {
 			/* Not filter error */
 			if (errs) {
 				free(*errs);
@@ -570,6 +658,8 @@ static int controlled_write(int fd, const char **filters,
 			}
 			return 1;
 		}
+		if (r < 0)
+			add_errors(errs, filter, ret--);
 	}
 	return ret;
 }
@@ -579,7 +669,11 @@ static int init_func_filter(struct func_filter *func_filter, const char *filter)
 	char *str;
 	int ret;
 
-	str = make_regex(filter);
+	if (!(func_filter->is_regex = is_regex(filter)))
+		str = make_regex(filter);
+	else
+		str = update_regex(filter);
+
 	if (!str)
 		return -1;
 
@@ -679,24 +773,27 @@ int tracefs_function_filter(struct tracefs_instance *instance, const char **filt
 		*errs = NULL;
 
 	ret = check_available_filters(func_filters, module, errs);
-	free_func_filters(func_filters);
 	if (ret)
-		return ret;
+		goto out_free;
 
+	ret = 1;
 	ftrace_filter_path = tracefs_instance_get_file(instance, TRACE_FILTER);
 	if (!ftrace_filter_path)
-		return 1;
+		goto out_free;
 
 	flags = reset ? O_TRUNC : O_APPEND;
 
 	fd = open(ftrace_filter_path, O_WRONLY | flags);
 	tracefs_put_tracing_file(ftrace_filter_path);
 	if (fd < 0)
-		return 1;
+		goto out_free;
 
-	ret = controlled_write(fd, filters, module, errs);
+	ret = controlled_write(fd, func_filters, module, errs);
 
 	close(fd);
 
+ out_free:
+	free_func_filters(func_filters);
+
 	return ret;
 }
-- 
2.30.1



  parent reply	other threads:[~2021-03-30  0:53 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-30  0:51 [PATCH 00/13 v2] libtracefs: Add tracefs_function_filter() Steven Rostedt
2021-03-30  0:51 ` [PATCH 01/13 v2] libtracefs: An API to set the filtering of functions Steven Rostedt
2021-03-30  0:51 ` [PATCH 02/13 v2] libtracefs: Document function_filter API Steven Rostedt
2021-03-30  0:51 ` [PATCH 03/13 v2] libtracefs: Fix notations of tracefs_function_filter() and module parameter Steven Rostedt
2021-03-30  0:51 ` [PATCH 04/13 v2] libtracefs: Move opening of file out of controlled_write() Steven Rostedt
2021-03-30  0:51 ` [PATCH 05/13 v2] libtracefs: Add add_errors() helper function for control_write() Steven Rostedt
2021-03-30  0:51 ` [PATCH 06/13 v2] libtracefs: Add checking of available_filter_functions to tracefs_function_filter() Steven Rostedt
2021-03-30  0:51 ` [PATCH 07/13 v2] libtracefs: Add write_filter() helper function Steven Rostedt
2021-03-30  0:51 ` Steven Rostedt [this message]
2021-04-01 16:33   ` [PATCH 08/13 v2] libtracefs: Allow for setting filters with regex expressions sameeruddin shaik
2021-03-31 16:39     ` Steven Rostedt
2021-04-02  1:59       ` sameeruddin shaik
2021-04-01  2:41         ` Steven Rostedt
2021-03-30  0:51 ` [PATCH 09/13 v2] libtracefs: Add indexing to set functions in tracefs_function_filter() Steven Rostedt
2021-03-30  0:51 ` [PATCH 10/13 v2] libtracefs: Pass in reset via flags to tracefs_function_filter() Steven Rostedt
2021-03-30 14:29   ` Tzvetomir Stoyanov
2021-03-30 14:53     ` Steven Rostedt
2021-03-30  0:51 ` [PATCH 11/13 v2] libtracefs: Add pthread_mutex_lock() around tracefs_function_filter() Steven Rostedt
2021-03-30  0:51 ` [PATCH 12/13 v2] libtracefs: Move struct tracefs_instance to tracefs-local.h Steven Rostedt
2021-03-30  0:51 ` [PATCH 13/13 v2] libtracefs: Add CONTINUE to tracefs_function_filter() Steven Rostedt
2021-03-30 14:29   ` Tzvetomir Stoyanov
2021-03-30 14:52     ` Steven Rostedt
2021-03-30 15:14       ` Steven Rostedt
2021-03-30 15:32       ` Tzvetomir Stoyanov
2021-03-30 16:03         ` Steven Rostedt
2021-03-30  1:03 ` [RFC][PATCH 14/13 v2] libtracefs: Just past one filter in for tracefs_function_filter() Steven Rostedt
2021-03-30 14:31   ` Tzvetomir Stoyanov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210330005248.391852830@goodmis.org \
    --to=rostedt@goodmis.org \
    --cc=linux-trace-devel@vger.kernel.org \
    --cc=sameeruddin.shaik8@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).