tracing/function-graph-tracer: make set_graph_function file support ftrace regex
PATCH 1/1 tracing/function-graph-tracer: make set_graph_function file support ftrace regex
Impact: trace only functions matching a pattern
The set_graph_function file let one to trace only one or several chosen functions and follow all their code flow.
Currently, only a constant function name is allowed so this patch allows the ftrace_regex functions:
_ matches all functions that end with "name":
echo *name > set_graph_function
_ matches all functions that begin with "name":
echo name* > set_graph_function
_ matches all functions that contains "name":
echo *name* > set_graph_function
Example:
echo mutex* > set_graph_function
0) | mutex_lock_nested() {
0) 0.563 us | __might_sleep();
0) 2.072 us | }
0) | mutex_unlock() {
0) 1.036 us | __mutex_unlock_slowpath();
0) 2.433 us | }
0) | mutex_unlock() {
0) 0.691 us | __mutex_unlock_slowpath();
0) 1.787 us | }
0) | mutex_lock_interruptible_nested() {
0) 0.548 us | __might_sleep();
0) 1.945 us | }--- kernel/trace/ftrace.c | 56 +++++++++++++++++++++++++++++++++--------------- 1 files changed, 38 insertions(+), 18 deletions(-)
kernel/trace/ftrace.c +38 -18
@@ -1895,6 +1895,10 @@ static void *g_start(struct seq_file *m, loff_t *pos)mutex_lock (&graph_lock); +/* Nothing, tell g_show to print all functions are enabled */ +if (!ftrace_graph_count && !*pos) +return (void *)1 ; + p =g_next (m, p, pos);return p;
@@ -1913,6 +1917,11 @@ static int g_show(struct seq_file *m, void *v)if (!ptr)return 0 ; +if (ptr == (unsigned long *)1 ) { +seq_printf (m,"#### all functions enabled ####\n" ); +return 0 ; + } +kallsyms_lookup (*ptr,NULL ,NULL ,NULL , str);seq_printf (m,"%s\n" , str);
@@ -1966,38 +1975,51 @@ ftrace_graph_read(struct file *file, char __user *ubuf, }static int -ftrace_set_func (unsigned long *array,int idx,char *buffer) +ftrace_set_func (unsigned long *array,int *idx,char *buffer) { -char str[KSYM_SYMBOL_LEN ];struct dyn_ftrace *rec;struct ftrace_page *pg; +int search_len;int found =0 ; -int j; +int type, not; +char *search; +bool exists; +int i;if (ftrace_disabled)return -ENODEV ; +/* decode regex */ + type =ftrace_setup_glob (buffer,strlen (buffer), &search, ¬); +if (not) +return -EINVAL ; + + search_len =strlen (search); +mutex_lock (&ftrace_lock);do_for_each_ftrace_rec (pg, rec) { +if (*idx >=FTRACE_GRAPH_MAX_FUNCS ) +break ; +if (rec->flags & (FTRACE_FL_FAILED |FTRACE_FL_FREE ))continue ; -kallsyms_lookup (rec->ip,NULL ,NULL ,NULL , str); -if (strcmp (str, buffer) ==0 ) { -/* Return 1 if we add it to the array */ - found =1 ; -for (j =0 ; j < idx; j++) -if (array[j] == rec->ip) { - found =0 ; +if (ftrace_match_record (rec, search, search_len, type)) { +/* ensure it is not already in the array */ + exists = false; +for (i =0 ; i < *idx; i++) +if (array[i] == rec->ip) { + exists = true;break ; } -if (found) - array[idx] = rec->ip; - goto out; +if (!exists) { + array[(*idx)++] = rec->ip; + found =1 ; + } } }while_for_each_ftrace_rec (); - out: +mutex_unlock (&ftrace_lock);return found ?0 : -EINVAL ;
@@ -2066,13 +2088,11 @@ ftrace_graph_write(struct file *file, const char __user *ubuf, } buffer[index] =0 ; -/* we allow only one at a time */ - ret =ftrace_set_func (array, ftrace_graph_count, buffer); +/* we allow only one expression at a time */ + ret =ftrace_set_func (array, &ftrace_graph_count, buffer);if (ret) goto out; - ftrace_graph_count++; - file->f_pos += read; ret = read;
Signature · 1 line
1.6.1
Ingo Molnar
AppliedQuoted 34 lines · Frederic Weisbecker
Impact: trace only functions matching a pattern
The
set_graph_functionfile let one to trace only one or several chosen functions and follow all their code flow.Currently, only a constant function name is allowed so this patch allows the
ftrace_regexfunctions:_ matches all functions that end with "name":
echo *name >set_graph_function_ matches all functions that begin with "name":
echo name* >set_graph_function_ matches all functions that contains "name":
echo *name* >set_graph_functionExample:
echo mutex* >
set_graph_function0) | mutex_lock_nested() { 0) 0.563 us | __might_sleep(); 0) 2.072 us | } 0) | mutex_unlock() { 0) 1.036 us | __mutex_unlock_slowpath(); 0) 2.433 us | } 0) | mutex_unlock() { 0) 0.691 us | __mutex_unlock_slowpath(); 0) 1.787 us | } 0) | mutex_lock_interruptible_nested() { 0) 0.548 us | __might_sleep(); 0) 1.945 us | }
Quoted 4 lines
Signed-off-by Frederic Weisbecker <[email protected]>--- kernel/trace/ftrace.c | 56 +++++++++++++++++++++++++++++++++--------------- 1 files changed, 38 insertions(+), 18 deletions(-)
Applied, thanks Frederic!
I'm wondering about the following: the whole set_graph_function method is limited (to FTRACE_GRAPH_MAX_FUNCS, 32 entries), is not scalable (we walk the array of functions at every function trace point), has a separate API, etc.
Wouldnt we off better with a good, generic function attributes hash, and an extension to the generic regexp parser to enable the setting of those attributes? One such attribute could be the 'expand child function'
Ingo
Quoted 49 lines · Ingo Molnar
Quoted 34 lines · Frederic Weisbecker
Impact: trace only functions matching a pattern
The
set_graph_functionfile let one to trace only one or several chosen functions and follow all their code flow.Currently, only a constant function name is allowed so this patch allows the
ftrace_regexfunctions:_ matches all functions that end with "name":
echo *name >set_graph_function_ matches all functions that begin with "name":
echo name* >set_graph_function_ matches all functions that contains "name":
echo *name* >set_graph_functionExample:
echo mutex* >
set_graph_function0) | mutex_lock_nested() { 0) 0.563 us | __might_sleep(); 0) 2.072 us | } 0) | mutex_unlock() { 0) 1.036 us | __mutex_unlock_slowpath(); 0) 2.433 us | } 0) | mutex_unlock() { 0) 0.691 us | __mutex_unlock_slowpath(); 0) 1.787 us | } 0) | mutex_lock_interruptible_nested() { 0) 0.548 us | __might_sleep(); 0) 1.945 us | }Quoted 4 lines
Signed-off-by Frederic Weisbecker <[email protected]>--- kernel/trace/ftrace.c | 56 +++++++++++++++++++++++++++++++++--------------- 1 files changed, 38 insertions(+), 18 deletions(-)Applied, thanks Frederic!
I'm wondering about the following: the whole
set_graph_functionmethod is limited (toFTRACE_GRAPH_MAX_FUNCS, 32 entries), is not scalable (we walk the array of functions at every function trace point), has a separate API, etc.
Quoted 6 lines
Wouldnt we off better with a good, generic function attributes hash, and an extension to the generic regexp parser to enable the setting of those attributes? One such attribute could be the 'expand child function'
Ingo
And well, you are answering this question I couldn't solve yesterday. One of my goal was to be able to trace easily the syscalls:
echo sys_* > set_graph_function
But this array is static. And making it runtime resizable resulted in other problems:
locking? but doing that for each function is unacceptable. rcu was a good candidate but I wonder that rcu would have been traced too, occuring tracing recursion.
Well, using such generic hashtable with such an attribute is really a good idea.
Quoted 152 lines · Frederic Weisbecker
Impact: trace only functions matching a pattern
The
set_graph_functionfile let one to trace only one or several chosen functions and follow all their code flow.Currently, only a constant function name is allowed so this patch allows the
ftrace_regexfunctions:_ matches all functions that end with "name":
echo *name >set_graph_function_ matches all functions that begin with "name":
echo name* >set_graph_function_ matches all functions that contains "name":
echo *name* >set_graph_functionExample:
echo mutex* >
set_graph_function0) | mutex_lock_nested() { 0) 0.563 us | __might_sleep(); 0) 2.072 us | } 0) | mutex_unlock() { 0) 1.036 us | __mutex_unlock_slowpath(); 0) 2.433 us | } 0) | mutex_unlock() { 0) 0.691 us | __mutex_unlock_slowpath(); 0) 1.787 us | } 0) | mutex_lock_interruptible_nested() { 0) 0.548 us | __might_sleep(); 0) 1.945 us | }Signed-off-by Frederic Weisbecker <[email protected]>--- kernel/trace/ftrace.c | 56 +++++++++++++++++++++++++++++++++--------------- 1 files changed, 38 insertions(+), 18 deletions(-)kernel/trace/ftrace.c +38
-18@@ -1895,6 +1895,10 @@ static void *g_start(struct seq_file *m, loff_t *pos)mutex_lock (&graph_lock); +/* Nothing, tell g_show to print all functions are enabled */ +if (!ftrace_graph_count && !*pos) +return (void *)1 ; + p =g_next (m, p, pos);return p;@@ -1913,6 +1917,11 @@ static int g_show(struct seq_file *m, void *v)if (!ptr)return 0 ; +if (ptr == (unsigned long *)1 ) { +seq_printf (m,"#### all functions enabled ####\n" ); +return 0 ; + } +kallsyms_lookup (*ptr,NULL ,NULL ,NULL , str);seq_printf (m,"%s\n" , str);@@ -1966,38 +1975,51 @@ ftrace_graph_read(struct file *file, char __user *ubuf, }static int -ftrace_set_func (unsigned long *array,int idx,char *buffer) +ftrace_set_func (unsigned long *array,int *idx,char *buffer) { -char str[KSYM_SYMBOL_LEN ];struct dyn_ftrace *rec;struct ftrace_page *pg; +int search_len;int found =0 ; -int j; +int type, not; +char *search; +bool exists; +int i;if (ftrace_disabled)return -ENODEV ; +/* decode regex */ + type =ftrace_setup_glob (buffer,strlen (buffer), &search, ¬); +if (not) +return -EINVAL ; + + search_len =strlen (search); +mutex_lock (&ftrace_lock);do_for_each_ftrace_rec (pg, rec) { +if (*idx >=FTRACE_GRAPH_MAX_FUNCS ) +break ; +if (rec->flags & (FTRACE_FL_FAILED |FTRACE_FL_FREE ))continue ; -kallsyms_lookup (rec->ip,NULL ,NULL ,NULL , str); -if (strcmp (str, buffer) ==0 ) { -/* Return 1 if we add it to the array */ - found =1 ; -for (j =0 ; j < idx; j++) -if (array[j] == rec->ip) { - found =0 ; +if (ftrace_match_record (rec, search, search_len, type)) { +/* ensure it is not already in the array */ + exists = false; +for (i =0 ; i < *idx; i++) +if (array[i] == rec->ip) { + exists = true;break ; } -if (found) - array[idx] = rec->ip; - goto out; +if (!exists) { + array[(*idx)++] = rec->ip; + found =1 ; + } } }while_for_each_ftrace_rec (); - out: +mutex_unlock (&ftrace_lock);return found ?0 : -EINVAL ;@@ -2066,13 +2088,11 @@ ftrace_graph_write(struct file *file, const char __user *ubuf, } buffer[index] =0 ; -/* we allow only one at a time */ - ret =ftrace_set_func (array, ftrace_graph_count, buffer); +/* we allow only one expression at a time */ + ret =ftrace_set_func (array, &ftrace_graph_count, buffer);if (ret) goto out; - ftrace_graph_count++; - file->f_pos += read; ret = read;Signature · 1 line
1.6.1
BTW, I got something that looked like a false positive with checkpatch.pl on this patch:
ERROR: return is not a function, parentheses are not required #56: FILE: kernel/trace/ftrace.c:1900:
+ return (void *)1;
total: 1 errors, 0 warnings, 101 lines checked
0001-tracing-function-graph-tracer-make-set_graph_functi.patch has style problems, please review. If any of these errors
are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.
It is confusing a cast with a function call.