Skip to contentSkip to conversation

dynamic_debug: add wildcard support to filter files/functions/modules

PATCH Superseded Du, Changbin 5 participants 24 messages Jul 25, 2013 – Nov 16, 2013

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dynamic_debug: add wildcard support to filter files/functions/modules

From: "Du, Changbin" <[email protected]>

This patch add wildcard '*'(matches zero or more characters) and '?' (matches one character) support when qurying debug flags.

Now we can open debug messages using keywords. eg:
1. open debug logs in all usb drivers
echo "file drivers/usb/* +p" > <debugfs>/dynamic_debug/control
2. open debug logs for usb xhci code
echo "file *xhci* +p" > <debugfs>/dynamic_debug/control

---
 lib/dynamic_debug.c | 27 ++++++++++++++++++++++-----
 1 file changed, 22 insertions(+), 5 deletions(-)
lib/dynamic_debug.c +22 -5
index 99fec3a..cdcce58 100644
@@ -127,6 +127,21 @@ static void vpr_info_dq(const struct ddebug_query *query, const char *msg)
 		 query->first_lineno, query->last_lineno);
 }
 
+static int match_pattern(char *pat, char *str)
+{
+	switch (*pat) {
+	case '\0':
+		return !*str;
+	case '*':
+		return  match_pattern(pat+1, str) || 
+			(*str && match_pattern(pat, str+1));
+	case '?':
+		return *str && match_pattern(pat+1, str+1);
+	default:
+		return *pat == *str && match_pattern(pat+1, str+1);
+	}
+}
+
 /*
  * Search the tables for _ddebug's which match the given `query' and
  * apply the `flags' and `mask' to them.  Returns number of matching
@@ -147,7 +162,7 @@ static int ddebug_change(const struct ddebug_query *query,
 	list_for_each_entry(dt, &ddebug_tables, link) {
 
 		/* match against the module name */
-		if (query->module && strcmp(query->module, dt->mod_name))
+		if (query->module && !match_pattern(query->module, dt->mod_name))
 			continue;
 
 		for (i = 0; i < dt->num_ddebugs; i++) {
@@ -155,14 +170,16 @@ static int ddebug_change(const struct ddebug_query *query,
 
 			/* match against the source filename */
 			if (query->filename &&
-			    strcmp(query->filename, dp->filename) &&
-			    strcmp(query->filename, kbasename(dp->filename)) &&
-			    strcmp(query->filename, trim_prefix(dp->filename)))
+			    !match_pattern(query->filename, dp->filename) &&
+			    !match_pattern(query->filename, 
+			    		   kbasename(dp->filename)) &&
+			    !match_pattern(query->filename, 
+			    		   trim_prefix(dp->filename)))
 				continue;
 
 			/* match against the function */
 			if (query->function &&
-			    strcmp(query->function, dp->function))
+			    !match_pattern(query->function, dp->function))
 				continue;
 
 			/* match against the format */
Signature · 1 line
1.8.1.2
#1 lore raw
Quoted 4 lines · Changbin

From: "Du, Changbin" <[email protected]>

This patch add wildcard '*'(matches zero or more characters) and '?' (matches one character) support when qurying debug flags.

Seems very useful. Caveat below.

lib/dynamic_debug.c

Quoted 1 line
lib/dynamic_debug.c +0 -0

[]

Quoted 8 lines
lib/dynamic_debug.c +4 -0
@@ -127,6 +127,21 @@ static void vpr_info_dq(const struct ddebug_query *query, const char *msg)
 		 query->first_lineno, query->last_lineno);
 }
 
+static int match_pattern(char *pat, char *str)
+{
+	switch (*pat) {
+	case '\0':

lib/dynamic_debug.c static void vpr_info_dq(const struct ddebug_query *query, const char *msg) · line 143

Quoted 10 lines
lib/dynamic_debug.c +10 -0
+		return !*str;
+	case '*':
+		return  match_pattern(pat+1, str) || 
+			(*str && match_pattern(pat, str+1));
+	case '?':
+		return *str && match_pattern(pat+1, str+1);
+	default:
+		return *pat == *str && match_pattern(pat+1, str 1);
+	}
+}

What's the maximum length string used today?
On a very long pattern, can this recursion cause stack overflow?

Other than that, I like it.

maintainer
#2 lore raw
Quoted 30 lines · Joe Perches
Quoted 4 lines · Changbin

From: "Du, Changbin" <[email protected]>

This patch add wildcard '*'(matches zero or more characters) and '?' (matches one character) support when qurying debug flags.

Seems very useful. Caveat below.

lib/dynamic_debug.c

Quoted 1 line
lib/dynamic_debug.c +0 -0

[]

Quoted 8 lines
lib/dynamic_debug.c +4 -0
@@ -127,6 +127,21 @@ static void vpr_info_dq(const struct ddebug_query *query, const char *msg)
  		 query->first_lineno, query->last_lineno);
  }
 
+static int match_pattern(char *pat, char *str)
+{
+	switch (*pat) {
+	case '\0':

lib/dynamic_debug.c static void vpr_info_dq(const struct ddebug_query *query, const char *msg) · line 143

Quoted 10 lines
lib/dynamic_debug.c +10 -0
+		return !*str;
+	case '*':
+		return  match_pattern(pat+1, str) ||
+			(*str && match_pattern(pat, str+1));
+	case '?':
+		return *str && match_pattern(pat+1, str+1);
+	default:
+		return *pat == *str && match_pattern(pat+1, str 1);
+	}
+}

What's the maximum length string used today?
On a very long pattern, can this recursion cause stack overflow?

Quoted 1 line

Other than that, I like it.

The recursion here is a concern - especially since the 'pat' pointer is controlled from userspace. Maybe not at pretty, but this can easily be done in userspace. For example, to set all driver/usb* one could do something like:

grep "drivers/usb" control | awk -F ":| " '{ print "file " $1 " line " $2 }' | xargs -i -n1 echo {} +p > control

Thanks,

-Jason

#3 lore raw

PATCH v2 1/1 dynamic_debug: add wildcard support to filter files/functions/modules

From: "Du, Changbin" <[email protected]>

This patch add wildcard '*'(matches zero or more characters) and '?' (matches one character) support when qurying debug flags.

Now we can open debug messages using keywords. eg:
1. open debug logs in all usb drivers
echo "file drivers/usb/* +p" > <debugfs>/dynamic_debug/control
2. open debug logs for usb xhci code
echo "file *xhci* +p" > <debugfs>/dynamic_debug/control

---
changes since v1:
rewrite match_pattern using non-recursion method.

---
 lib/dynamic_debug.c | 47 ++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 42 insertions(+), 5 deletions(-)
lib/dynamic_debug.c +42 -5
index c37aeac..d239207 100644
@@ -127,6 +127,41 @@ static void vpr_info_dq(const struct ddebug_query *query, const char *msg)
 		 query->first_lineno, query->last_lineno);
 }
 
+/* check if the string matches given pattern which includes wildcards */
+static int match_pattern(const char *pattern, const char *string)
+{
+	const char *s, *p;
+	int star = 0;
+
+loop:
+	for (s = string, p = pattern; *s; ++s, ++p) {
+		switch (*p) {
+		case '?':
+			break;
+		case '*':
+			star = 1;
+			string = s;
+			pattern = p;
+			if (!*++pattern)
+				return 1;
+			goto loop;
+		default:
+			if (*s != *p)
+				goto star_check;
+			break;
+		}
+	}
+	if (*p == '*')
+		++p;
+	return (!*p);
+
+star_check:
+	if (!star)
+		return 0;
+	string++;
+	goto loop;
+}
+
 /*
  * Search the tables for _ddebug's which match the given `query' and
  * apply the `flags' and `mask' to them.  Returns number of matching
@@ -147,7 +182,7 @@ static int ddebug_change(const struct ddebug_query *query,
 	list_for_each_entry(dt, &ddebug_tables, link) {
 
 		/* match against the module name */
-		if (query->module && strcmp(query->module, dt->mod_name))
+		if (query->module && !match_pattern(query->module, dt->mod_name))
 			continue;
 
 		for (i = 0; i < dt->num_ddebugs; i++) {
@@ -155,14 +190,16 @@ static int ddebug_change(const struct ddebug_query *query,
 
 			/* match against the source filename */
 			if (query->filename &&
-			    strcmp(query->filename, dp->filename) &&
-			    strcmp(query->filename, kbasename(dp->filename)) &&
-			    strcmp(query->filename, trim_prefix(dp->filename)))
+			    !match_pattern(query->filename, dp->filename) &&
+			    !match_pattern(query->filename,
+					   kbasename(dp->filename)) &&
+			    !match_pattern(query->filename,
+					   trim_prefix(dp->filename)))
 				continue;
 
 			/* match against the function */
 			if (query->function &&
-			    strcmp(query->function, dp->function))
+			    !match_pattern(query->function, dp->function))
 				continue;
 
 			/* match against the format */
Signature · 1 line
1.8.1.2
#4 lore raw
Quoted 1 line · Changbin

From: "Du, Changbin" <[email protected]>

trivial notes:

lib/dynamic_debug.c

Quoted 4 lines

This patch add wildcard '*'(matches zero or more characters) and '?' (matches one character) support when qurying debug flags.

lib/dynamic_debug.c +0 -0

[]

lib/dynamic_debug.c static void vpr_info_dq(const struct ddebug_query *query, const char *msg) · line 131

Quoted 6 lines
lib/dynamic_debug.c +2 -0
@@ -127,6 +127,41 @@ static void vpr_info_dq(const struct ddebug_query *query, const char *msg)
 		 query->first_lineno, query->last_lineno);
 }
 
+/* check if the string matches given pattern which includes wildcards */
+static int match_pattern(const char *pattern, const char *string)

bool match_pattern

lib/dynamic_debug.c static void vpr_info_dq(const struct ddebug_query *query, const char *msg) · line 134

Quoted 3 lines
lib/dynamic_debug.c +3 -0
+{
+	const char *s, *p;
+	int star = 0;

bool star = false;

Quoted 6 lines
lib/dynamic_debug.c +6 -0
+
+loop:
+	for (s = string, p = pattern; *s; ++s, ++p) {
+		switch (*p) {
+		case '?':
+			break;

lib/dynamic_debug.c static void vpr_info_dq(const struct ddebug_query *query, const char *msg) · line 150

Quoted 10 lines
lib/dynamic_debug.c +10 -0
+		case '*':
+			star = 1;
+			string = s;
+			pattern = p;
+			if (!*++pattern)
+				return 1;
+			goto loop;
+		default:
+			if (*s != *p)
+				goto star_check;
			star = false;

lib/dynamic_debug.c static void vpr_info_dq(const struct ddebug_query *query, const char *msg) · line 156

Quoted 6 lines
lib/dynamic_debug.c +6 -0
+			break;
+		}
+	}
+	if (*p == '*')
+		++p;
+	return (!*p);

Don't need parentheses.

lib/dynamic_debug.c static void vpr_info_dq(const struct ddebug_query *query, const char *msg) · line 163

Quoted 7 lines
lib/dynamic_debug.c +7 -0
+
+star_check:
+	if (!star)
+		return 0;
+	string++;
+	goto loop;
+}

The star_check: label block seems like it'd be
better in the switch case as it's only used once.

Maybe the thing would be more readable with a
while loop

#5 lore raw

Hello, Joe,

Thanks for your comments. I will update the patch. And now I am trying to remove all the two goto statements. Agree with you, we can use a while statement instead.

I will send you the new patch for you to review when it's done.

2013/10/29 Joe Perches <[email protected]>

Quoted 68 lines · Changbin
Quoted 1 line

From: "Du, Changbin" <[email protected]>

trivial notes:

lib/dynamic_debug.c

Quoted 4 lines

This patch add wildcard '*'(matches zero or more characters) and '?' (matches one character) support when qurying debug flags.

lib/dynamic_debug.c +0 -0

[]

lib/dynamic_debug.c static void vpr_info_dq(const struct ddebug_query *query, const char *msg) · line 131

Quoted 6 lines
lib/dynamic_debug.c +2 -0
@@ -127,6 +127,41 @@ static void vpr_info_dq(const struct ddebug_query *query, const char *msg)
               query->first_lineno, query->last_lineno);
 }
 
+/* check if the string matches given pattern which includes wildcards */
+static int match_pattern(const char *pattern, const char *string)

bool match_pattern

lib/dynamic_debug.c static void vpr_info_dq(const struct ddebug_query *query, const char *msg) · line 134

Quoted 3 lines
lib/dynamic_debug.c +3 -0
+{
+     const char *s, *p;
+     int star = 0;

bool star = false;

Quoted 6 lines
lib/dynamic_debug.c +6 -0
+
+loop:
+     for (s = string, p = pattern; *s; ++s, ++p) {
+             switch (*p) {
+             case '?':
+                     break;

lib/dynamic_debug.c static void vpr_info_dq(const struct ddebug_query *query, const char *msg) · line 150

Quoted 10 lines
lib/dynamic_debug.c +10 -0
+             case '*':
+                     star = 1;
+                     string = s;
+                     pattern = p;
+                     if (!*++pattern)
+                             return 1;
+                     goto loop;
+             default:
+                     if (*s != *p)
+                             goto star_check;
                        star = false;

lib/dynamic_debug.c static void vpr_info_dq(const struct ddebug_query *query, const char *msg) · line 156

Quoted 6 lines
lib/dynamic_debug.c +6 -0
+                     break;
+             }
+     }
+     if (*p == '*')
+             ++p;
+     return (!*p);

Don't need parentheses.

lib/dynamic_debug.c static void vpr_info_dq(const struct ddebug_query *query, const char *msg) · line 163

Quoted 7 lines
lib/dynamic_debug.c +7 -0
+
+star_check:
+     if (!star)
+             return 0;
+     string++;
+     goto loop;
+}

The star_check: label block seems like it'd be
better in the switch case as it's only used once.

Maybe the thing would be more readable with a
while loop

#6 lore raw

PATCH v3 1/1 dynamic_debug: add wildcard support to filter files/functions/modules

From: "Du, Changbin" <[email protected]>

This patch add wildcard '*'(matches zero or more characters) and '?' (matches one character) support when qurying debug flags.

Now we can open debug messages using keywords. eg:
1. open debug logs in all usb drivers
echo "file drivers/usb/* +p" > <debugfs>/dynamic_debug/control
2. open debug logs for usb xhci code
echo "file *xhci* +p" > <debugfs>/dynamic_debug/control

---
changes since v2:
remove the goto statements.
code style issue.

---
 lib/dynamic_debug.c | 49 ++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 44 insertions(+), 5 deletions(-)
lib/dynamic_debug.c +44 -5
index c37aeac..b166d19 100644
@@ -127,6 +127,43 @@ static void vpr_info_dq(const struct ddebug_query *query, const char *msg)
 		 query->first_lineno, query->last_lineno);
 }
 
+/* check if the string matches given pattern which includes wildcards */
+static bool match_pattern(const char *pattern, const char *string)
+{
+	const char *s = string,
+		   *p = pattern;
+	bool star = 0;
+
+	while (*s) {
+		switch (*p) {
+		case '?':
+			++s, ++p;
+			break;
+		case '*':
+			star = true;
+			string = s;
+			if (!*++p)
+				return true;
+			pattern = p;;
+			break;
+		default:
+			if (*s != *p) {
+				if (!star)
+					return false;
+				string++;
+				s = string;
+				p = pattern;
+				break;
+			}
+			++s, ++p;
+		}
+	}
+
+	if (*p == '*')
+		++p;
+	return !*p;
+}
+
 /*
  * Search the tables for _ddebug's which match the given `query' and
  * apply the `flags' and `mask' to them.  Returns number of matching
@@ -147,7 +184,7 @@ static int ddebug_change(const struct ddebug_query *query,
 	list_for_each_entry(dt, &ddebug_tables, link) {
 
 		/* match against the module name */
-		if (query->module && strcmp(query->module, dt->mod_name))
+		if (query->module && !match_pattern(query->module, dt->mod_name))
 			continue;
 
 		for (i = 0; i < dt->num_ddebugs; i++) {
@@ -155,14 +192,16 @@ static int ddebug_change(const struct ddebug_query *query,
 
 			/* match against the source filename */
 			if (query->filename &&
-			    strcmp(query->filename, dp->filename) &&
-			    strcmp(query->filename, kbasename(dp->filename)) &&
-			    strcmp(query->filename, trim_prefix(dp->filename)))
+			    !match_pattern(query->filename, dp->filename) &&
+			    !match_pattern(query->filename,
+					   kbasename(dp->filename)) &&
+			    !match_pattern(query->filename,
+					   trim_prefix(dp->filename)))
 				continue;
 
 			/* match against the function */
 			if (query->function &&
-			    strcmp(query->function, dp->function))
+			    !match_pattern(query->function, dp->function))
 				continue;
 
 			/* match against the format */
Signature · 1 line
1.8.1.2
#7 lore raw
Quoted 2 lines · Changbin

This patch add wildcard '*'(matches zero or more characters) and '?' (matches one character) support when qurying debug flags.

Hi again. Some trivial notes and a possible logic error:

lib/dynamic_debug.c

Quoted 1 line
lib/dynamic_debug.c +0 -0

[]

lib/dynamic_debug.c static void vpr_info_dq(const struct ddebug_query *query, const char *msg) · line 134

Quoted 9 lines
lib/dynamic_debug.c +5 -0
@@ -127,6 +127,43 @@ static void vpr_info_dq(const struct ddebug_query *query, const char *msg)
 		 query->first_lineno, query->last_lineno);
 }
 
+/* check if the string matches given pattern which includes wildcards */
+static bool match_pattern(const char *pattern, const char *string)
+{
+	const char *s = string,
+		   *p = pattern;

This sort of alignment is pretty unusual. Most kernel uses just repeat the type like:

	const char *s = string;
	const char *p = pattern;

lib/dynamic_debug.c static void vpr_info_dq(const struct ddebug_query *query, const char *msg) · line 135

Quoted 1 line
lib/dynamic_debug.c +1 -0
+	bool star = 0;
	bool star = false;

lib/dynamic_debug.c static void vpr_info_dq(const struct ddebug_query *query, const char *msg) · line 147

Quoted 12 lines
lib/dynamic_debug.c +12 -0
+
+	while (*s) {
+		switch (*p) {
+		case '?':
+			++s, ++p;
+			break;
+		case '*':
+			star = true;
+			string = s;
+			if (!*++p)
+				return true;
+			pattern = p;;

repeated ;
Running your patches through checkpatch should find this sort of defect.

lib/dynamic_debug.c static void vpr_info_dq(const struct ddebug_query *query, const char *msg) · line 158

Quoted 11 lines
lib/dynamic_debug.c +11 -0
+			break;
+		default:
+			if (*s != *p) {
+				if (!star)
+					return false;
+				string++;
+				s = string;
+				p = pattern;
+				break;
+			}
+			++s, ++p;

Maybe nicer with an if/else, I think you're still missing a reset of "star = false;" and I also think it's better to use a break here too.

		if (*s == *p) {
			s++;
			p++;
			star = false;
		} else {
			if (!star)
				return false;
			string++;
			s = string;
			p = pattern;
		}
		break;
#8 lore raw

2013/10/30 Joe Perches <[email protected]>:

Quoted 16 lines · Changbin
Quoted 2 lines

This patch add wildcard '*'(matches zero or more characters) and '?' (matches one character) support when qurying debug flags.

Hi again. Some trivial notes and a possible logic error:

lib/dynamic_debug.c static void vpr_info_dq(const struct ddebug_query *query, const char *msg) · line 134

Quoted 5 lines
lib/dynamic_debug.c +5 -0
+/* check if the string matches given pattern which includes wildcards */
+static bool match_pattern(const char *pattern, const char *string)
+{
+     const char *s = string,
+                *p = pattern;

This sort of alignment is pretty unusual. Most kernel uses just repeat the type like:

Quoted 3 lines
        const char *s = string;
        const char *p = pattern;

I think so.

Quoted 17 lines
Quoted 1 line

+ bool star = 0;

        bool star = false;

lib/dynamic_debug.c static void vpr_info_dq(const struct ddebug_query *query, const char *msg) · line 147

Quoted 12 lines
lib/dynamic_debug.c +12 -0
+
+     while (*s) {
+             switch (*p) {
+             case '?':
+                     ++s, ++p;
+                     break;
+             case '*':
+                     star = true;
+                     string = s;
+                     if (!*++p)
+                             return true;
+                     pattern = p;;
Quoted 4 lines

repeated ;
Running your patches through checkpatch should find this sort of defect.

Sorry, it's may fault. I forgot to check it using the tool.

Quoted 16 lines

lib/dynamic_debug.c static void vpr_info_dq(const struct ddebug_query *query, const char *msg) · line 158

Quoted 11 lines
lib/dynamic_debug.c +11 -0
+                     break;
+             default:
+                     if (*s != *p) {
+                             if (!star)
+                                     return false;
+                             string++;
+                             s = string;
+                             p = pattern;
+                             break;
+                     }
+                     ++s, ++p;

Maybe nicer with an if/else, I think you're still missing a reset of "star = false;" and I also think it's better to use a break here too.

Quoted 12 lines
                if (*s == *p) {
                        s++;
                        p++;
                        star = false;
                } else {
                        if (!star)
                                return false;
                        string++;
                        s = string;
                        p = pattern;
                }
                break;

I have run loss of test before sending patch. all case passed. But I will double check if need reset star flag. really thanks!

#9 lore raw

2013/10/30 Changbin Du <[email protected]>:

Quoted 24 lines

2013/10/30 Joe Perches <[email protected]>:

Quoted 10 lines · Changbin
Quoted 2 lines

This patch add wildcard '*'(matches zero or more characters) and '?' (matches one character) support when qurying debug flags.

Hi again. Some trivial notes and a possible logic error:

Maybe nicer with an if/else, I think you're still missing a reset of "star = false;" and I also think it's better to use a break here too.

Quoted 12 lines
                if (*s == *p) {
                        s++;
                        p++;
                        star = false;
                } else {
                        if (!star)
                                return false;
                        string++;
                        s = string;
                        p = pattern;
                }
                break;
Quoted 2 lines

I have run loss of test before sending patch. all case passed. But I will double check if need reset star flag. really thanks!

Hi, Joe. I checked this. The "star = false;" can not have here. Attachment is a test program that I use it to test the algorithm. it will compare this non-recursion and old recursion if they are equal.

Now I will send the v3 patch, please help to review. Thanks!

#include <stdio.h>
#include <stdlib.h>

#define bool int
#define false 0
#define true 1

static int match_pattern1(char *pat, char *str)

{
	switch (*pat) {
	case '\0':
		return !*str;
	case '*':
		return  match_pattern1(pat+1, str) ||
		        (*str && match_pattern1(pat, str+1));
	case '?':
		return *str && match_pattern1(pat+1, str+1);
	default:
		return *pat == *str && match_pattern1(pat+1, str+1);
	}
}

static bool match_pattern2(const char *pattern, const char *string)

{
	const char *s;
	const char *p;
	bool star = false;

loop:

	for (s = string, p = pattern; *s; ++s, ++p) {
		switch (*p) {
		case '?':
			break;
		case '*':
			star = true;
			string = s;
			pattern = p;
			if (!*++pattern)
				return 1;
			goto loop;
		default:
			if (*s != *p)
				goto star_check;
			break;
		}
	}
	if (*p == '*')
		++p;
	return (!*p);

star_check:

	if (!star)
		return 0;
	string++;
	goto loop;
}

static bool match_pattern3(const char *pattern, const char *string)

{
	const char *s = string;
	const char *p = pattern;
	bool star = false;

	while (*s) {
		switch (*p) {
		case '?':
			s++;
			p++;
			break;
		case '*':
			star = true;
			string = s;
			if (!*++p)
				return true;
			pattern = p;
			break;
		default:
			if (*s == *p) {
				s++;
				p++;
			} else {
				if (!star)
					return false;
				string++;
				s = string;
				p = pattern;
			}
			break;
		}
	}

	if (*p == '*')
		++p;
	return !*p;
}

/* return 0 if all the result of three function is same, else return 1. */ static int test_match()

{
	char *Str[] = { "ZIP",
	                ".ZIP",
	                "ZIP.",
	                "afdZfdaZIP"
	                "adfsafZfdsadfIfdafdsPfdasdf",
	                "accbddcrrfddhuulggnffphhqggyyyrnnvhgflllmmnnnnkpi.iuuuiyt",
	                "A.bkdfadfasfa.faskfa.sfaf?kl.A.ZIP",
	                "Asdhgerlthwegjsdklgjsdgjsdkgjsdgjsdg.ZIP",
	                "AAgsdjtweoruterjtertweiutwejtwejtwetwoejtwejtrwleAA.ZIP",
	                "Agjsdgjdsjgsdkjgsdjgsjd?gjsd?gjsd?gj?sdgj.A.ZIdgjsdkjglsdjgPPO",
	                "fasdfe323rerteter55rtrewtrwwe"
	              };
	char *Patt[] = { "?ZIP", "*ZIP", "ZIP.", "*ZIP", "*?ZIP", "?*?ZIP", "*?*?ZIP", "*Z*I*P*",
	                 "*?*?*ZIP", "a*?*?*ZIP", "*ZI?", "*ZI?", "*ZI*", "*ZI*",
	                 "a*?*?", "?*?*","ZIP", "ZIP", "AAZIP", "AZIP", "AAAAZIP", "AAZIPPO"
	               };
	int i = 0, j = 0, ret = 0;

	for (i = 0 ; i < sizeof(Patt)/sizeof(Patt[0]); i++) {
		for (j = 0; j < sizeof(Str)/sizeof(Str[0]); j++) {
			char *pat = Patt[i];
			char *str = Str[j];
			bool match1 = match_pattern1(pat, str);
			bool match2 = match_pattern2(pat, str);
			bool match3 = match_pattern3(pat, str);

			printf("\"%s\" =?= \"%s\"\n", pat, str);
			if (match1 == match2 && match2 == match3)
				printf("All three result is same: %d\n", match1);
			else {
				printf("****\n"
				       "****Someone is wrong: match1=%d match2=%d match3=%d\n"
				       "****\n",
				       match1, match2, match3);
				ret = 1;
			}
			printf("\n");
		}
		printf("\n");
	}
	return ret;
}

int main()

{
	return test_match();
}
#10 lore raw

PATCH v4 1/2 dynamic_debug: add wildcard support to filter files/functions/modules

From: "Du, Changbin" <[email protected]>

This patch add wildcard '*'(matches zero or more characters) and '?' (matches one character) support when qurying debug flags.

Now we can open debug messages using keywords. eg:
1. open debug logs in all usb drivers
echo "file drivers/usb/* +p" > <debugfs>/dynamic_debug/control
2. open debug logs for usb xhci code
echo "file *xhci* +p" > <debugfs>/dynamic_debug/control

---
 lib/dynamic_debug.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 49 insertions(+), 5 deletions(-)
lib/dynamic_debug.c +49 -5
index c37aeac..b953780 100644
@@ -8,6 +8,7 @@
  * By Greg Banks <[email protected]>
  * Copyright (c) 2008 Silicon Graphics Inc.  All Rights Reserved.
  * Copyright (C) 2011 Bart Van Assche.  All Rights Reserved.
+ * Copyright (C) 2013 Du, Changbin <[email protected]>
  */
 
 #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
@@ -127,6 +128,46 @@ static void vpr_info_dq(const struct ddebug_query *query, const char *msg)
 		 query->first_lineno, query->last_lineno);
 }
 
+/* check if the string matches given pattern which includes wildcards */
+static bool match_pattern(const char *pattern, const char *string)
+{
+	const char *s = string;
+	const char *p = pattern;
+	bool star = false;
+
+	while (*s) {
+		switch (*p) {
+		case '?':
+			s++;
+			p++;
+			break;
+		case '*':
+			star = true;
+			string = s;
+			if (!*++p)
+				return true;
+			pattern = p;
+			break;
+		default:
+			if (*s == *p) {
+				s++;
+				p++;
+			} else {
+				if (!star)
+					return false;
+				string++;
+				s = string;
+				p = pattern;
+			}
+			break;
+		}
+	}
+
+	if (*p == '*')
+		++p;
+	return !*p;
+}
+
 /*
  * Search the tables for _ddebug's which match the given `query' and
  * apply the `flags' and `mask' to them.  Returns number of matching
@@ -147,7 +188,8 @@ static int ddebug_change(const struct ddebug_query *query,
 	list_for_each_entry(dt, &ddebug_tables, link) {
 
 		/* match against the module name */
-		if (query->module && strcmp(query->module, dt->mod_name))
+		if (query->module &&
+		    !match_pattern(query->module, dt->mod_name))
 			continue;
 
 		for (i = 0; i < dt->num_ddebugs; i++) {
@@ -155,14 +197,16 @@ static int ddebug_change(const struct ddebug_query *query,
 
 			/* match against the source filename */
 			if (query->filename &&
-			    strcmp(query->filename, dp->filename) &&
-			    strcmp(query->filename, kbasename(dp->filename)) &&
-			    strcmp(query->filename, trim_prefix(dp->filename)))
+			    !match_pattern(query->filename, dp->filename) &&
+			    !match_pattern(query->filename,
+					   kbasename(dp->filename)) &&
+			    !match_pattern(query->filename,
+					   trim_prefix(dp->filename)))
 				continue;
 
 			/* match against the function */
 			if (query->function &&
-			    strcmp(query->function, dp->function))
+			    !match_pattern(query->function, dp->function))
 				continue;
 
 			/* match against the format */
Signature · 1 line
1.8.1.2
#11 lore raw

PATCH v4 2/2 dynamic-debug-howto.txt: update since new wildcard support

From: "Du, Changbin" <[email protected]>

Add the usage of using new feature wildcard support.

---
 Documentation/dynamic-debug-howto.txt | 9 +++++++++
 1 file changed, 9 insertions(+)
Documentation/dynamic-debug-howto.txt +9 -0
index 1bbdcfc..46325eb 100644
@@ -108,6 +108,12 @@ If your query set is big, you can batch them too:
 
   ~# cat query-batch-file > <debugfs>/dynamic_debug/control
 
+A another way is to use wildcard. The match rule support '*' (matches
+zero or more characters) and '?' (matches exactly one character).For
+example, you can match all usb drivers:
+
+  ~# echo "file drivers/usb/* +p" > <debugfs>/dynamic_debug/control
+
 At the syntactical level, a command comprises a sequence of match
 specifications, followed by a flags change specification.
 
@@ -315,6 +321,9 @@ nullarbor:~ # echo -n 'func svc_process -p' >
 nullarbor:~ # echo -n 'format "nfsd: READ" +p' >
 				<debugfs>/dynamic_debug/control
 
+// enable messages in files of which the pathes include string "usb"
+nullarbor:~ # echo -n '*usb* +p' > <debugfs>/dynamic_debug/control
+
 // enable all messages
 nullarbor:~ # echo -n '+p' > <debugfs>/dynamic_debug/control
 
Signature · 1 line
1.8.1.2
#12 lore raw
In reply to Du, Changbin

Hi Changbin,

Quoted 8 lines

This patch add wildcard '*'(matches zero or more characters) and '?' (matches one character) support when qurying debug flags.

Now we can open debug messages using keywords. eg:
1. open debug logs in all usb drivers
echo "file drivers/usb/* +p" > <debugfs>/dynamic_debug/control
2. open debug logs for usb xhci code
echo "file *xhci* +p" > <debugfs>/dynamic_debug/control

and this should have a section in Documentation/dynamic-debug-howto.txt.

Regards

Marcel

#13 lore raw

2013/10/30 Marcel Holtmann <[email protected]>:

Quoted 5 lines

Hi Changbin,

Quoted 3 lines

This patch add wildcard '*'(matches zero or more characters) and '?' (matches one character) support when qurying debug flags.

Quoted 7 lines
Quoted 5 lines

Now we can open debug messages using keywords. eg:
1. open debug logs in all usb drivers
echo "file drivers/usb/* +p" > <debugfs>/dynamic_debug/control
2. open debug logs for usb xhci code
echo "file *xhci* +p" > <debugfs>/dynamic_debug/control

and this should have a section in Documentation/dynamic-debug-howto.txt.

Yes, I will add a section to update the documentation also. Thanks for reminding.

Quoted 5 lines

Regards

Marcel

maintainer
#14 lore raw
In reply to Du, Changbin
Quoted 8 lines · Changbin

This patch add wildcard '*'(matches zero or more characters) and '?' (matches one character) support when qurying debug flags.

Now we can open debug messages using keywords. eg:
1. open debug logs in all usb drivers
echo "file drivers/usb/* +p" > <debugfs>/dynamic_debug/control
2. open debug logs for usb xhci code
echo "file *xhci* +p" > <debugfs>/dynamic_debug/control

This patch would be a lot easier to understand (and hence review) if you had remembered to update Documentation/dynamic-debug-howto.txt!

lib/dynamic_debug.c static void vpr_info_dq(const struct ddebug_query *query, const char *msg) · line 131

Quoted 8 lines
lib/dynamic_debug.c +2 -0
@@ -127,6 +127,41 @@ static void vpr_info_dq(const struct ddebug_query *query, const char *msg)
 		 query->first_lineno, query->last_lineno);
 }
 
+/* check if the string matches given pattern which includes wildcards */
+static int match_pattern(const char *pattern, const char *string)

No, something like this should be in lib/ so that other callers can use it. We already have at least one copy handy in drivers/ata/libata-core.c:glob_match(). A better approach would be to move that glob_match() into lib/glob_match.c then teach dynamic_debug to use it.

There are probably other private globbing functions lying around the kernel, but it's rather a hard thing to grep for...

#15 lore raw
Quoted 1 line · Andrew Morton

On Mon, 28 Oct 2013 23:29:10 +0800 "Du, Changbin" <[email protected]> wrote:

[]

Quoted 2 lines

lib/dynamic_debug.c static void vpr_info_dq(const struct ddebug_query *query, const char *msg) · line 131

Quoted 2 lines
lib/dynamic_debug.c +2 -0
+/* check if the string matches given pattern which includes wildcards */
+static int match_pattern(const char *pattern, const char *string)

[]

Quoted 8 lines

No, something like this should be in lib/ so that other callers can use it. We already have at least one copy handy in drivers/ata/libata-core.c:glob_match(). A better approach would be to move that glob_match() into lib/glob_match.c then teach dynamic_debug to use it.

There are probably other private globbing functions lying around the kernel, but it's rather a hard thing to grep for...

Maybe use lib/parser.c where the other match_<foo> functions are already.

match_glob has the disadvantage that it's recursive too.

trace has:

kernel/trace/trace_events_filter.c:static int regex_match_full(char *str, struct regex *r, int len) kernel/trace/trace_events_filter.c:static int regex_match_front(char *str, struct regex *r, int len) kernel/trace/trace_events_filter.c:static int regex_match_middle(char *str, struct regex *r, int len) kernel/trace/trace_events_filter.c:static int regex_match_end(char *str, struct regex *r, int len)

but there probably aren't many more that could be converted.

#16 lore raw

2013/11/1 Joe Perches <[email protected]>:

Quoted 12 lines · Andrew Morton
Quoted 1 line · Changbin

[]

Quoted 2 lines

lib/dynamic_debug.c static void vpr_info_dq(const struct ddebug_query *query, const char *msg) · line 131

Quoted 2 lines
lib/dynamic_debug.c +2 -0
+/* check if the string matches given pattern which includes wildcards */
+static int match_pattern(const char *pattern, const char *string)

[]

Quoted 6 lines

No, something like this should be in lib/ so that other callers can use it. We already have at least one copy handy in drivers/ata/libata-core.c:glob_match(). A better approach would be to move that glob_match() into lib/glob_match.c then teach dynamic_debug to use it.

Quoted 5 lines
Quoted 2 lines

There are probably other private globbing functions lying around the kernel, but it's rather a hard thing to grep for...

Maybe use lib/parser.c where the other match_<foo> functions are already.

match_<foo> functions in lib/parser.c just do simple match, they doesn't support wildcards.
So it's not useful for us.

Quoted 3 lines

match_glob has the disadvantage that it's recursive too.

As you mentioned before, we cannot use it since strings are given from userspace. :)

Quoted 8 lines

trace has:

kernel/trace/trace_events_filter.c:static int regex_match_full(char *str, struct regex *r, int len) kernel/trace/trace_events_filter.c:static int regex_match_front(char *str, struct regex *r, int len) kernel/trace/trace_events_filter.c:static int regex_match_middle(char *str, struct regex *r, int len) kernel/trace/trace_events_filter.c:static int regex_match_end(char *str, struct regex *r, int len)

but there probably aren't many more that could be converted.

Trace filter support wildcards, but it seems the match algorithm couples with trace code. I'll try to see
if it can be split.

#17 lore raw
In reply to Joe Perches

2013/11/1 Joe Perches <[email protected]>:

Quoted 12 lines · Andrew Morton
Quoted 1 line · Changbin

[]

Quoted 2 lines

lib/dynamic_debug.c static void vpr_info_dq(const struct ddebug_query *query, const char *msg) · line 131

Quoted 2 lines
lib/dynamic_debug.c +2 -0
+/* check if the string matches given pattern which includes wildcards */
+static int match_pattern(const char *pattern, const char *string)

[]

Quoted 6 lines

No, something like this should be in lib/ so that other callers can use it. We already have at least one copy handy in drivers/ata/libata-core.c:glob_match(). A better approach would be to move that glob_match() into lib/glob_match.c then teach dynamic_debug to use it.

Quoted 5 lines
Quoted 2 lines

There are probably other private globbing functions lying around the kernel, but it's rather a hard thing to grep for...

Maybe use lib/parser.c where the other match_<foo> functions are already.

match_<foo> functions in lib/parser.c just do simple match, they doesn't support wildcards.
So it's not useful for us.

Quoted 3 lines

match_glob has the disadvantage that it's recursive too.

As you mentioned before, we cannot use it since strings are given from userspace. :)

Quoted 8 lines

trace has:

kernel/trace/trace_events_filter.c:static int regex_match_full(char *str, struct regex *r, int len) kernel/trace/trace_events_filter.c:static int regex_match_front(char *str, struct regex *r, int len) kernel/trace/trace_events_filter.c:static int regex_match_middle(char *str, struct regex *r, int len) kernel/trace/trace_events_filter.c:static int regex_match_end(char *str, struct regex *r, int len)

but there probably aren't many more that could be converted.

Trace filter support wildcards, but it seems the match algorithm couples with trace code. I'll try to see
if it can be split.

#18 lore raw
Quoted 16 lines · Changbin Du

2013/11/1 Joe Perches <[email protected]>:

Quoted 15 lines · Andrew Morton
Quoted 1 line · Changbin

[]

Quoted 2 lines

lib/dynamic_debug.c static void vpr_info_dq(const struct ddebug_query *query, const char *msg) · line 131

Quoted 2 lines
lib/dynamic_debug.c +2 -0
+/* check if the string matches given pattern which includes wildcards */
+static int match_pattern(const char *pattern, const char *string)

[]

Quoted 8 lines

No, something like this should be in lib/ so that other callers can use it. We already have at least one copy handy in drivers/ata/libata-core.c:glob_match(). A better approach would be to move that glob_match() into lib/glob_match.c then teach dynamic_debug to use it.

There are probably other private globbing functions lying around the kernel, but it's rather a hard thing to grep for...

Quoted 6 lines
Quoted 2 lines

Maybe use lib/parser.c where the other match_<foo> functions are already.

match_<foo> functions in lib/parser.c just do simple match, they doesn't support wildcards.
So it's not useful for us.

It's not meant to be useful so much as be a possible generic location for your match_regex function.

#19 lore raw

2013/11/7 Joe Perches <[email protected]>:

Quoted 9 lines · Changbin Du
Quoted 4 lines

2013/11/1 Joe Perches <[email protected]>:
match_<foo> functions in lib/parser.c just do simple match, they doesn't support wildcards.
So it's not useful for us.

It's not meant to be useful so much as be a possible generic location for your match_regex function.

I misunderstood you. This is a appropriate file to place the matching function. I have moved it to this file.

I checked the regex_match_foo functions in trace_events_filter.c. Per my reading, I think it's a little tedious, and only support '*' character. They are glued with tracing framework. So i gave up to re-used them.

I will send new version patch set to you later.

Thanks!
Du, Changbin

#20 lore raw

v5 cover add wildcard support for dynamic debug

From: "Du, Changbin" <[email protected]>

These patches are to make it easier to filter kernel debug logs which we want.
Whith wildcard support, below command can enable all usb debug logs:
#echo "file drivers/usb/* +p" > <debugfs>/dynamic_debug/control
This patch only enables two wildcard:
'*' - matches zero or more characters
'?' - matches one character

Changes since v4:
moved matching function to lib/parser.c

Du, Changbin (3):
lib/parser.c: add match_wildcard function
dynamic_debug: add wildcard support to filter files/functions/modules dynamic-debug-howto.txt: update since new wildcard support

 Documentation/dynamic-debug-howto.txt |  9 +++++++
 include/linux/parser.h                |  1 +
 lib/dynamic_debug.c                   | 15 +++++++----
 lib/parser.c                          | 51 +++++++++++++++++++++++++++++++++++
 4 files changed, 71 insertions(+), 5 deletions(-)
Signature · 1 line
1.8.3.2
#21 lore raw

PATCH v5 1/3 lib/parser.c: add match_wildcard function

From: "Du, Changbin" <[email protected]>

match_wildcard function is a simple implementation of wildcard matching algorithm. It only supports two usual wildcardes:
'*' - matches zero or more characters
'?' - matches one character
This algorithm is safe since it's of non-recursion.

---
 include/linux/parser.h |  1 +
 lib/parser.c           | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 52 insertions(+)
include/linux/parser.h +1 -0
index ea2281e..39d5b79 100644
@@ -29,5 +29,6 @@ int match_token(char *, const match_table_t table, substring_t args[]);
 int match_int(substring_t *, int *result);
 int match_octal(substring_t *, int *result);
 int match_hex(substring_t *, int *result);
+bool match_wildcard(const char *pattern, const char *str);
 size_t match_strlcpy(char *, const substring_t *, size_t);
 char *match_strdup(const substring_t *);
lib/parser.c +51 -0

62 lines not on this page · raw message

Signature · 1 line
1.8.3.2
#22 lore raw

PATCH v5 2/3 dynamic_debug: add wildcard support to filter files/functions/modules

In reply to Du, Changbin

From: "Du, Changbin" <[email protected]>

Add wildcard '*'(matches zero or more characters) and '?' (matches one character) support when qurying debug flags.

Now we can open debug messages using keywords. eg:
1. open debug logs in all usb drivers
echo "file drivers/usb/* +p" > <debugfs>/dynamic_debug/control
2. open debug logs for usb xhci code
echo "file *xhci* +p" > <debugfs>/dynamic_debug/control

---
 lib/dynamic_debug.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
lib/dynamic_debug.c +10 -5
index c37aeac..600ac57 100644
@@ -8,6 +8,7 @@
  * By Greg Banks <[email protected]>
  * Copyright (c) 2008 Silicon Graphics Inc.  All Rights Reserved.
  * Copyright (C) 2011 Bart Van Assche.  All Rights Reserved.
+ * Copyright (C) 2013 Du, Changbin <[email protected]>
  */
 
 #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
@@ -24,6 +25,7 @@
 #include <linux/sysctl.h>
 #include <linux/ctype.h>
 #include <linux/string.h>
+#include <linux/parser.h>
 #include <linux/string_helpers.h>
 #include <linux/uaccess.h>
 #include <linux/dynamic_debug.h>
@@ -147,7 +149,8 @@ static int ddebug_change(const struct ddebug_query *query,
 	list_for_each_entry(dt, &ddebug_tables, link) {
 
 		/* match against the module name */
-		if (query->module && strcmp(query->module, dt->mod_name))
+		if (query->module &&
+		    !match_wildcard(query->module, dt->mod_name))
 			continue;
 
 		for (i = 0; i < dt->num_ddebugs; i++) {
@@ -155,14 +158,16 @@ static int ddebug_change(const struct ddebug_query *query,
 
 			/* match against the source filename */
 			if (query->filename &&
-			    strcmp(query->filename, dp->filename) &&
-			    strcmp(query->filename, kbasename(dp->filename)) &&
-			    strcmp(query->filename, trim_prefix(dp->filename)))
+			    !match_wildcard(query->filename, dp->filename) &&
+			    !match_wildcard(query->filename,
+					   kbasename(dp->filename)) &&
+			    !match_wildcard(query->filename,
+					   trim_prefix(dp->filename)))
 				continue;
 
 			/* match against the function */
 			if (query->function &&
-			    strcmp(query->function, dp->function))
+			    !match_wildcard(query->function, dp->function))
 				continue;
 
 			/* match against the format */
Signature · 1 line
1.8.3.2
#23 lore raw

PATCH v5 3/3 dynamic-debug-howto.txt: update since new wildcard support

In reply to Du, Changbin

From: "Du, Changbin" <[email protected]>

Add the usage of using new feature wildcard support.

---
 Documentation/dynamic-debug-howto.txt | 9 +++++++++
 1 file changed, 9 insertions(+)
Documentation/dynamic-debug-howto.txt +9 -0
index 1bbdcfc..46325eb 100644
@@ -108,6 +108,12 @@ If your query set is big, you can batch them too:
 
   ~# cat query-batch-file > <debugfs>/dynamic_debug/control
 
+A another way is to use wildcard. The match rule support '*' (matches
+zero or more characters) and '?' (matches exactly one character).For
+example, you can match all usb drivers:
+
+  ~# echo "file drivers/usb/* +p" > <debugfs>/dynamic_debug/control
+
 At the syntactical level, a command comprises a sequence of match
 specifications, followed by a flags change specification.
 
@@ -315,6 +321,9 @@ nullarbor:~ # echo -n 'func svc_process -p' >
 nullarbor:~ # echo -n 'format "nfsd: READ" +p' >
 				<debugfs>/dynamic_debug/control
 
+// enable messages in files of which the pathes include string "usb"
+nullarbor:~ # echo -n '*usb* +p' > <debugfs>/dynamic_debug/control
+
 // enable all messages
 nullarbor:~ # echo -n '+p' > <debugfs>/dynamic_debug/control
 
Signature · 1 line
1.8.3.2