libbpf: kprobe.multi: cross filter using available_filter_functions and kallsyms
PATCH v3 1/2 libbpf: kprobe.multi: cross filter using available_filter_functions and kallsyms
From: Jackie Liu <[email protected]>
When using regular expression matching with "kprobe multi", it scans all the functions under "/proc/kallsyms" that can be matched. However, not all of them can be traced by kprobe.multi. If any one of the functions fails to be traced, it will result in the failure of all functions. The best approach is to filter out the functions that cannot be traced to ensure proper tracking of the functions.
---
v2->v3: fix 'fscanf' may overflow
tools/lib/bpf/libbpf.c | 122 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 109 insertions(+), 13 deletions(-)
tools/lib/bpf/libbpf.c +109 -13
@@ -10224,6 +10224,12 @@ static const char *tracefs_uprobe_events(void)return use_debugfs () ?DEBUGFS "/uprobe_events" :TRACEFS "/uprobe_events" ; } +static const char *tracefs_available_filter_functions (void ) +{ +return use_debugfs () ?DEBUGFS "/available_filter_functions" : +TRACEFS "/available_filter_functions" ; +} +static void gen_kprobe_legacy_event_name (char *buf,size_t buf_sz,const char *kfunc_name,size_t offset) {
@@ -10539,23 +10545,113 @@ struct kprobe_multi_resolve {size_t cnt; }; -static int -resolve_kprobe_multi_cb (unsigned long long sym_addr,char sym_type, -const char *sym_name,void *ctx) +static int qsort_compare_function (const void *a,const void *b) { -struct kprobe_multi_resolve *res = ctx; -int err; +return strcmp (*(const char **)a, *(const char **)b); +} -if (!glob_match (sym_name, res->pattern)) -return 0 ; +static int bsearch_compare_function (const void *a,const void *b) +{ +return strcmp ((const char *)a, *(const char **)b); +} - err =libbpf_ensure_mem ((void **) &res->addrs, &res->cap,sizeof (unsigned long ), - res->cnt +1 ); -if (err) +static int libbpf_available_kallsyms_parse (struct kprobe_multi_resolve *res) +{ +char sym_name[500 ]; +const char *available_functions_file =tracefs_available_filter_functions (); +FILE *f; +int err =0 , ret, i; +struct function_info { +const char **syms; +size_t cap; +size_t cnt; + } infos = {}; + + f =fopen (available_functions_file,"r" ); +if (!f) { + err = -errno; +pr_warn ("failed to open %s\n" , available_functions_file);return err; + } - res->addrs[res->cnt++] = (unsigned long ) sym_addr; -return 0 ; +while (true) { +char *name; + + ret =fscanf (f,"%499s%*[^\n]\n" , sym_name); +if (ret ==EOF &&feof (f)) +break ; + +if (ret !=1 ) { +pr_warn ("failed to read available function file entry: %d\n" , + ret); + err = -EINVAL ; + goto cleanup; + } + +if (!glob_match (sym_name, res->pattern)) +continue ; + + err =libbpf_ensure_mem ((void **)&infos.syms, &infos.cap, +sizeof (void *), infos.cnt +1 ); +if (err) + goto cleanup; + + name =strdup (sym_name); +if (!name) { + err = -errno; + goto cleanup; + } + + infos.syms[infos.cnt++] = name; + } +fclose (f); + +/* sort available functions */ +qsort (infos.syms, infos.cnt,sizeof (void *), qsort_compare_function); + + f =fopen ("/proc/kallsyms" ,"r" ); +if (!f) { + err = -errno; +pr_warn ("failed to open /proc/kallsyms\n" ); + goto free_infos; + } + +while (true) { +unsigned long long sym_addr; + + ret =fscanf (f,"%llx %*c %499s%*[^\n]\n" , &sym_addr, sym_name); +if (ret ==EOF &&feof (f)) +break ; + +if (ret !=2 ) { +pr_warn ("failed to read kallsyms entry: %d\n" , ret); + err = -EINVAL ; +break ; + } + +if (!glob_match (sym_name, res->pattern)) +continue ; + +if (!bsearch (&sym_name, infos.syms, infos.cnt,sizeof (void *), + bsearch_compare_function)) +continue ; + + err =libbpf_ensure_mem ((void **)&res->addrs, &res->cap, +sizeof (unsigned long ), res->cnt +1 ); +if (err) +break ; + + res->addrs[res->cnt++] = (unsigned long ) sym_addr; + } + +cleanup: +fclose (f); +free_infos: +for (i =0 ; i < infos.cnt; i++) +free ((char *)infos.syms[i]); +free (infos.syms); + +return err; } struct bpf_link *
@@ -10594,7 +10690,7 @@ bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,return libbpf_err_ptr (-EINVAL );if (pattern) { - err =libbpf_kallsyms_parse (resolve_kprobe_multi_cb, &res); + err =libbpf_available_kallsyms_parse (&res);if (err) goto error;if (!res.cnt) {
Signature · 1 line
2.25.1
PATCH v3 2/2 libbpf: kprobe.multi: Filter with available_filter_functions_addrs
From: Jackie Liu <[email protected]>
Now, we provide a new available_filter_functions_addrs interface, which can help us not need to cross-validate available_filter_functions and kallsyms, which can effectively improve efficiency. For example, on my device, the sample program [1] of start time:
$ sudo ./funccount "tcp_*"
before after
1.2s 1.0s
[1]: https://github.com/JackieLiu1/ketones/tree/master/src/funccount
---
v2->v3: fix 'fscanf' may overflow
tools/lib/bpf/libbpf.c | 58 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +57 -1
@@ -10230,6 +10230,12 @@ static const char *tracefs_available_filter_functions(void)TRACEFS "/available_filter_functions" ; } +static const char *tracefs_available_filter_functions_addrs (void ) +{ +return use_debugfs () ?DEBUGFS "/available_filter_functions_addrs" : +TRACEFS "/available_filter_functions_addrs" ; +} +static void gen_kprobe_legacy_event_name (char *buf,size_t buf_sz,const char *kfunc_name,size_t offset) {
@@ -10654,6 +10660,53 @@ static int libbpf_available_kallsyms_parse(struct kprobe_multi_resolve *res)return err; } +static bool has_available_filter_functions_addrs (void ) +{ +return access (tracefs_available_filter_functions_addrs (),R_OK ) !=-1 ; +} + +static int libbpf_available_kprobes_parse (struct kprobe_multi_resolve *res) +{ +char sym_name[500 ]; +FILE *f; +int ret, err =0 ; +unsigned long long sym_addr; +const char *available_path =tracefs_available_filter_functions_addrs (); + + f =fopen (available_path,"r" ); +if (!f) { + err = -errno; +pr_warn ("failed to open %s.\n" , available_path); +return err; + } + +while (true) { + ret =fscanf (f,"%llx %499s%*[^\n]\n" , &sym_addr, sym_name); +if (ret ==EOF &&feof (f)) +break ; + +if (ret !=2 ) { +pr_warn ("failed to read available kprobe entry: %d\n" , + ret); + err = -EINVAL ; +break ; + } + +if (!glob_match (sym_name, res->pattern)) +continue ; + + err =libbpf_ensure_mem ((void **) &res->addrs, &res->cap, +sizeof (unsigned long ), res->cnt +1 ); +if (err) +break ; + + res->addrs[res->cnt++] = (unsigned long ) sym_addr; + } + +fclose (f); +return err; +} +struct bpf_link * bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog, const char *pattern,
@@ -10690,7 +10743,10 @@ bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,return libbpf_err_ptr (-EINVAL );if (pattern) { - err =libbpf_available_kallsyms_parse (&res); +if (has_available_filter_functions_addrs ()) + err =libbpf_available_kprobes_parse (&res); +else + err =libbpf_available_kallsyms_parse (&res);if (err) goto error;if (!res.cnt) {
Signature · 1 line
2.25.1
Quoted 62 lines · Jackie Liu
From: Jackie Liu <[email protected]>
When using regular expression matching with "kprobe multi", it scans all the functions under "
/proc/kallsyms" that can be matched. However, not all of them can be traced by kprobe.multi. If any one of the functions fails to be traced, it will result in the failure of all functions. The best approach is to filter out the functions that cannot be traced to ensure proper tracking of the functions.Reported-by kernel test robot <[email protected]>Suggested-by Jiri Olsa <[email protected]>Suggested-by Andrii Nakryiko <[email protected]>Signed-off-by Jackie Liu <[email protected]>---
v2->v3: fix 'fscanf' may overflowtools/lib/bpf/libbpf.c | 122 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 109 insertions(+), 13 deletions(-)tools/lib/bpf/libbpf.c +14
-10@@ -10224,6 +10224,12 @@ static const char *tracefs_uprobe_events(void)return use_debugfs () ?DEBUGFS "/uprobe_events" :TRACEFS "/uprobe_events" ; } +static const char *tracefs_available_filter_functions (void ) +{ +return use_debugfs () ?DEBUGFS "/available_filter_functions" : +TRACEFS "/available_filter_functions" ; +} +static void gen_kprobe_legacy_event_name (char *buf,size_t buf_sz,const char *kfunc_name,size_t offset) {@@ -10539,23 +10545,113 @@ struct kprobe_multi_resolve {size_t cnt; }; -static int -resolve_kprobe_multi_cb (unsigned long long sym_addr,char sym_type, -const char *sym_name,void *ctx) +static int qsort_compare_function (const void *a,const void *b) { -struct kprobe_multi_resolve *res = ctx; -int err; +return strcmp (*(const char **)a, *(const char **)b); +} -if (!glob_match (sym_name, res->pattern)) -return 0 ; +static int bsearch_compare_function (const void *a,const void *b) +{ +return strcmp ((const char *)a, *(const char **)b); +} - err =libbpf_ensure_mem ((void **) &res->addrs, &res->cap,sizeof (unsigned long ), - res->cnt +1 ); -if (err) +static int libbpf_available_kallsyms_parse (struct kprobe_multi_resolve *res)
tools/lib/bpf/libbpf.c struct kprobe_multi_resolve { · line 10568
Quoted 10 lines
tools/lib/bpf/libbpf.c +10
-0+{ +char sym_name[500 ]; +const char *available_functions_file =tracefs_available_filter_functions (); +FILE *f; +int err =0 , ret, i; +struct function_info { +const char **syms; +size_t cap; +size_t cnt; + } infos = {};
do you need to define new struct for this? there's just on infos variable of that, you could use just:
const char **syms = NULL; size_t cap = 0, cnt = 0;
Quoted 31 lines
tools/lib/bpf/libbpf.c +27
-2+ + f =fopen (available_functions_file,"r" ); +if (!f) { + err = -errno; +pr_warn ("failed to open %s\n" , available_functions_file);return err; + } - res->addrs[res->cnt++] = (unsigned long ) sym_addr; -return 0 ; +while (true) { +char *name; + + ret =fscanf (f,"%499s%*[^\n]\n" , sym_name); +if (ret ==EOF &&feof (f)) +break ; + +if (ret !=1 ) { +pr_warn ("failed to read available function file entry: %d\n" , + ret); + err = -EINVAL ; + goto cleanup; + } + +if (!glob_match (sym_name, res->pattern)) +continue ; + + err =libbpf_ensure_mem ((void **)&infos.syms, &infos.cap, +sizeof (void *), infos.cnt +1 ); +if (err) + goto cleanup;
tools/lib/bpf/libbpf.c struct kprobe_multi_resolve { · line 10607
Quoted 10 lines
tools/lib/bpf/libbpf.c +10
-0+ + name =strdup (sym_name); +if (!name) { + err = -errno; + goto cleanup; + } + + infos.syms[infos.cnt++] = name; + } +fclose (f);
should you check if you found anything (infos.cnt != 0) and return early if there's nothing found
tools/lib/bpf/libbpf.c struct kprobe_multi_resolve { · line 10612
Quoted 5 lines
tools/lib/bpf/libbpf.c +5
-0+ +/* sort available functions */ +qsort (infos.syms, infos.cnt,sizeof (void *), qsort_compare_function); + + f =fopen ("/proc/kallsyms" ,"r" );
why not use libbpf_kallsyms_parse for kallsyms parsing? the call below would be in its callback
Quoted 11 lines
tools/lib/bpf/libbpf.c +11
-0+if (!f) { + err = -errno; +pr_warn ("failed to open /proc/kallsyms\n" ); + goto free_infos; + } + +while (true) { +unsigned long long sym_addr; + + ret =fscanf (f,"%llx %*c %499s%*[^\n]\n" , &sym_addr, sym_name); +if (ret ==EOF &&feof (f))
tools/lib/bpf/libbpf.c struct kprobe_multi_resolve { · line 10633
Quoted 10 lines
tools/lib/bpf/libbpf.c +10
-0+break ; + +if (ret !=2 ) { +pr_warn ("failed to read kallsyms entry: %d\n" , ret); + err = -EINVAL ; +break ; + } + +if (!glob_match (sym_name, res->pattern)) +continue ;
hm, we don't need to call glob_match again, we just want to check if the kallsyms symbol is in infos.syms
tools/lib/bpf/libbpf.c struct kprobe_multi_resolve { · line 10645
Quoted 12 lines
tools/lib/bpf/libbpf.c +12
-0+ +if (!bsearch (&sym_name, infos.syms, infos.cnt,sizeof (void *), + bsearch_compare_function)) +continue ; + + err =libbpf_ensure_mem ((void **)&res->addrs, &res->cap, +sizeof (unsigned long ), res->cnt +1 ); +if (err) +break ; + + res->addrs[res->cnt++] = (unsigned long ) sym_addr; + }
res->cnt is check outside for 0, so we should be find here
jirka
Quoted 24 lines
tools/lib/bpf/libbpf.c +10
-1+ +cleanup: +fclose (f); +free_infos: +for (i =0 ; i < infos.cnt; i++) +free ((char *)infos.syms[i]); +free (infos.syms); + +return err; } struct bpf_link *@@ -10594,7 +10690,7 @@ bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,return libbpf_err_ptr (-EINVAL );if (pattern) { - err =libbpf_kallsyms_parse (resolve_kprobe_multi_cb, &res); + err =libbpf_available_kallsyms_parse (&res);if (err) goto error;if (!res.cnt) {Signature · 1 line
2.25.1
Quoted 125 lines · Jiri Olsa
Quoted 62 lines · Jackie Liu
From: Jackie Liu <[email protected]>
When using regular expression matching with "kprobe multi", it scans all the functions under "
/proc/kallsyms" that can be matched. However, not all of them can be traced by kprobe.multi. If any one of the functions fails to be traced, it will result in the failure of all functions. The best approach is to filter out the functions that cannot be traced to ensure proper tracking of the functions.Reported-by kernel test robot <[email protected]>Suggested-by Jiri Olsa <[email protected]>Suggested-by Andrii Nakryiko <[email protected]>Signed-off-by Jackie Liu <[email protected]>---
v2->v3: fix 'fscanf' may overflowtools/lib/bpf/libbpf.c | 122 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 109 insertions(+), 13 deletions(-)tools/lib/bpf/libbpf.c +14
-10@@ -10224,6 +10224,12 @@ static const char *tracefs_uprobe_events(void)return use_debugfs () ?DEBUGFS "/uprobe_events" :TRACEFS "/uprobe_events" ; } +static const char *tracefs_available_filter_functions (void ) +{ +return use_debugfs () ?DEBUGFS "/available_filter_functions" : +TRACEFS "/available_filter_functions" ; +} +static void gen_kprobe_legacy_event_name (char *buf,size_t buf_sz,const char *kfunc_name,size_t offset) {@@ -10539,23 +10545,113 @@ struct kprobe_multi_resolve {size_t cnt; }; -static int -resolve_kprobe_multi_cb (unsigned long long sym_addr,char sym_type, -const char *sym_name,void *ctx) +static int qsort_compare_function (const void *a,const void *b) { -struct kprobe_multi_resolve *res = ctx; -int err; +return strcmp (*(const char **)a, *(const char **)b); +} -if (!glob_match (sym_name, res->pattern)) -return 0 ; +static int bsearch_compare_function (const void *a,const void *b) +{ +return strcmp ((const char *)a, *(const char **)b); +} - err =libbpf_ensure_mem ((void **) &res->addrs, &res->cap,sizeof (unsigned long ), - res->cnt +1 ); -if (err) +static int libbpf_available_kallsyms_parse (struct kprobe_multi_resolve *res)tools/lib/bpf/libbpf.c struct kprobe_multi_resolve { · line 10568
Quoted 10 lines
tools/lib/bpf/libbpf.c +10
-0+{ +char sym_name[500 ]; +const char *available_functions_file =tracefs_available_filter_functions (); +FILE *f; +int err =0 , ret, i; +struct function_info { +const char **syms; +size_t cap; +size_t cnt; + } infos = {};do you need to define new struct for this? there's just on infos variable of that, you could use just:
const char **syms = NULL; size_t cap = 0, cnt = 0;Quoted 31 lines
tools/lib/bpf/libbpf.c +27
-2+ + f =fopen (available_functions_file,"r" ); +if (!f) { + err = -errno; +pr_warn ("failed to open %s\n" , available_functions_file);return err; + } - res->addrs[res->cnt++] = (unsigned long ) sym_addr; -return 0 ; +while (true) { +char *name; + + ret =fscanf (f,"%499s%*[^\n]\n" , sym_name); +if (ret ==EOF &&feof (f)) +break ; + +if (ret !=1 ) { +pr_warn ("failed to read available function file entry: %d\n" , + ret); + err = -EINVAL ; + goto cleanup; + } + +if (!glob_match (sym_name, res->pattern)) +continue ; + + err =libbpf_ensure_mem ((void **)&infos.syms, &infos.cap, +sizeof (void *), infos.cnt +1 ); +if (err) + goto cleanup;tools/lib/bpf/libbpf.c struct kprobe_multi_resolve { · line 10607
Quoted 10 lines
tools/lib/bpf/libbpf.c +10
-0+ + name =strdup (sym_name); +if (!name) { + err = -errno; + goto cleanup; + } + + infos.syms[infos.cnt++] = name; + } +fclose (f);should you check if you found anything (infos.cnt != 0) and return early if there's nothing found
Quoted 8 lines
tools/lib/bpf/libbpf.c struct kprobe_multi_resolve { · line 10612
Quoted 5 lines
tools/lib/bpf/libbpf.c +5
-0+ +/* sort available functions */ +qsort (infos.syms, infos.cnt,sizeof (void *), qsort_compare_function); + + f =fopen ("/proc/kallsyms" ,"r" );why not use
libbpf_kallsyms_parsefor kallsyms parsing? the call below would be in its callback
This place cannot directly use libbpf_kallsyms_parse, because we need info.syms, this value cannot be passed into the parameters of libbpf_kallsyms_parse, and we cannot turn info.syms into a global variable, which is unnecessary. The easiest way is to reimplement a A copy of libbpf_kallsyms_parse.
Modifications to other parts will be carried along with the next version.
Signature · 1 line
Jackie
Quoted 67 lines
Quoted 12 lines
tools/lib/bpf/libbpf.c +11
-0+if (!f) { + err = -errno; +pr_warn ("failed to open /proc/kallsyms\n" ); + goto free_infos; + } + +while (true) { +unsigned long long sym_addr; + + ret =fscanf (f,"%llx %*c %499s%*[^\n]\n" , &sym_addr, sym_name); +if (ret ==EOF &&feof (f))tools/lib/bpf/libbpf.c struct kprobe_multi_resolve { · line 10633
Quoted 10 lines
tools/lib/bpf/libbpf.c +10
-0+break ; + +if (ret !=2 ) { +pr_warn ("failed to read kallsyms entry: %d\n" , ret); + err = -EINVAL ; +break ; + } + +if (!glob_match (sym_name, res->pattern)) +continue ;hm, we don't need to call
glob_matchagain, we just want to check if the kallsyms symbol is in infos.symstools/lib/bpf/libbpf.c struct kprobe_multi_resolve { · line 10645
Quoted 12 lines
tools/lib/bpf/libbpf.c +12
-0+ +if (!bsearch (&sym_name, infos.syms, infos.cnt,sizeof (void *), + bsearch_compare_function)) +continue ; + + err =libbpf_ensure_mem ((void **)&res->addrs, &res->cap, +sizeof (unsigned long ), res->cnt +1 ); +if (err) +break ; + + res->addrs[res->cnt++] = (unsigned long ) sym_addr; + }
res->cntis check outside for 0, so we should be find herejirka
Quoted 24 lines
tools/lib/bpf/libbpf.c +10
-1+ +cleanup: +fclose (f); +free_infos: +for (i =0 ; i < infos.cnt; i++) +free ((char *)infos.syms[i]); +free (infos.syms); + +return err; } struct bpf_link *@@ -10594,7 +10690,7 @@ bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,return libbpf_err_ptr (-EINVAL );if (pattern) { - err =libbpf_kallsyms_parse (resolve_kprobe_multi_cb, &res); + err =libbpf_available_kallsyms_parse (&res);if (err) goto error;if (!res.cnt) {Signature · 1 line
2.25.1
On Tue, Jul 04, 2023 at 09:33:15AM +0800, Jackie Liu wrote:
SNIP
Quoted 13 lines
Quoted 4 lines
should you check if you found anything (infos.cnt != 0) and return early if there's nothing found
Quoted 8 lines
tools/lib/bpf/libbpf.c struct kprobe_multi_resolve { · line 10612
Quoted 5 lines
tools/lib/bpf/libbpf.c +5
-0+ +/* sort available functions */ +qsort (infos.syms, infos.cnt,sizeof (void *), qsort_compare_function); + + f =fopen ("/proc/kallsyms" ,"r" );why not use
libbpf_kallsyms_parsefor kallsyms parsing? the call below would be in its callback
Quoted 3 lines
This place cannot directly use
libbpf_kallsyms_parse, because we need info.syms, this value cannot be passed into the parameters oflibbpf_kallsyms_parse,
hum, libbpf_kallsyms_parse takes 'void *ctx', so you can pass anything you want right?
thanks,
jirka
Quoted 75 lines
and we cannot turn info.syms into a global
variable, which is unnecessary. The easiest way is to reimplement a A copy oflibbpf_kallsyms_parse.Modifications to other parts will be carried along with the next version.
Signature · 1 line
JackieQuoted 65 lines
Quoted 12 lines
tools/lib/bpf/libbpf.c +11
-0+if (!f) { + err = -errno; +pr_warn ("failed to open /proc/kallsyms\n" ); + goto free_infos; + } + +while (true) { +unsigned long long sym_addr; + + ret =fscanf (f,"%llx %*c %499s%*[^\n]\n" , &sym_addr, sym_name); +if (ret ==EOF &&feof (f))tools/lib/bpf/libbpf.c struct kprobe_multi_resolve { · line 10633
Quoted 10 lines
tools/lib/bpf/libbpf.c +10
-0+break ; + +if (ret !=2 ) { +pr_warn ("failed to read kallsyms entry: %d\n" , ret); + err = -EINVAL ; +break ; + } + +if (!glob_match (sym_name, res->pattern)) +continue ;hm, we don't need to call
glob_matchagain, we just want to check if the kallsyms symbol is in infos.symstools/lib/bpf/libbpf.c struct kprobe_multi_resolve { · line 10645
Quoted 12 lines
tools/lib/bpf/libbpf.c +12
-0+ +if (!bsearch (&sym_name, infos.syms, infos.cnt,sizeof (void *), + bsearch_compare_function)) +continue ; + + err =libbpf_ensure_mem ((void **)&res->addrs, &res->cap, +sizeof (unsigned long ), res->cnt +1 ); +if (err) +break ; + + res->addrs[res->cnt++] = (unsigned long ) sym_addr; + }
res->cntis check outside for 0, so we should be find herejirka
Quoted 22 lines
tools/lib/bpf/libbpf.c +10
-1+ +cleanup: +fclose (f); +free_infos: +for (i =0 ; i < infos.cnt; i++) +free ((char *)infos.syms[i]); +free (infos.syms); + +return err; } struct bpf_link *@@ -10594,7 +10690,7 @@ bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,return libbpf_err_ptr (-EINVAL );if (pattern) { - err =libbpf_kallsyms_parse (resolve_kprobe_multi_cb, &res); + err =libbpf_available_kallsyms_parse (&res);if (err) goto error;if (!res.cnt) {Signature · 1 line
2.25.1
Quoted 17 lines · Jiri Olsa
On Tue, Jul 04, 2023 at 09:33:15AM +0800, Jackie Liu wrote:
SNIP
Quoted 13 lines
Quoted 12 lines
should you check if you found anything (infos.cnt != 0) and return early if there's nothing found
Review
Quoted 5 lines
(quoted diff) +5
-0+ + /* sort available functions */ + qsort(infos.syms, infos.cnt, sizeof(void *), qsort_compare_function); + + f = fopen("/proc/kallsyms", "r");why not use
libbpf_kallsyms_parsefor kallsyms parsing? the call below would be in its callback
Quoted 6 lines
Quoted 3 lines
This place cannot directly use
libbpf_kallsyms_parse, because we need info.syms, this value cannot be passed into the parameters oflibbpf_kallsyms_parse,hum,
libbpf_kallsyms_parsetakes 'void *ctx', so you can pass anything you want right?
somthing like below should save some lines and ease up error handling
I'd add similar parse functions for both available_filter_functions and available_filter_functions_addrs and add the logic to callbacks
jirka
---
tools/lib/bpf/libbpf.c +26 -32
@@ -10559,8 +10559,31 @@ static int bsearch_compare_function(const void *a, const void *b)return strcmp ((const char *)a, *(const char **)b); } +struct avail_kallsyms_data { +const char **syms; +struct kprobe_multi_resolve *res; +}; + +static int avail_kallsyms_cb (unsigned long long sym_addr,char sym_type, +const char *sym_name,void *ctx) +{ +struct avail_kallsyms_data *data = ctx; +struct kprobe_multi_resolve *res = data->res; + +if (!bsearch (&sym_name, data->syms, cnt,sizeof (void *), bsearch_compare_function)) +continue ; + + err =libbpf_ensure_mem ((void **)&res->addrs, &res->cap, +sizeof (unsigned long ), res->cnt +1 ); +if (err) +return err; + res->addrs[res->cnt++] = (unsigned long ) sym_addr; +return 0 ; +} +static int libbpf_available_kallsyms_parse (struct kprobe_multi_resolve *res) { +struct avail_kallsyms_data data;char sym_name[500 ];const char *available_functions_file =tracefs_available_filter_functions ();FILE *f;
@@ -10614,42 +10637,13 @@ static int libbpf_available_kallsyms_parse(struct kprobe_multi_resolve *res)/* sort available functions */ qsort (syms, cnt,sizeof (void *), qsort_compare_function); - f =fopen ("/proc/kallsyms" ,"r" ); -if (!f) { - err = -errno; -pr_warn ("failed to open /proc/kallsyms\n" ); - goto free_syms; - } - -while (true) { -unsigned long long sym_addr; - - ret =fscanf (f,"%llx %*c %499s%*[^\n]\n" , &sym_addr, sym_name); -if (ret ==EOF &&feof (f)) -break ; - -if (ret !=2 ) { -pr_warn ("failed to read kallsyms entry: %d\n" , ret); - err = -EINVAL ; - goto cleanup; - } - -if (!bsearch (&sym_name, syms, cnt,sizeof (void *), bsearch_compare_function)) -continue ; - - err =libbpf_ensure_mem ((void **)&res->addrs, &res->cap, -sizeof (unsigned long ), res->cnt +1 ); -if (err) - goto cleanup; - - res->addrs[res->cnt++] = (unsigned long ) sym_addr; - } + data.syms = syms; + data.res = res; +libbpf_kallsyms_parse (avail_kallsyms_cb, res);if (!res->cnt) err = -ENOENT ; -cleanup: -fclose (f); free_syms:for (i =0 ; i < cnt; i++)free ((char *)syms[i]);
Quoted 79 lines · Jackie Liu
From: Jackie Liu <[email protected]>
When using regular expression matching with "kprobe multi", it scans all the functions under "
/proc/kallsyms" that can be matched. However, not all of them can be traced by kprobe.multi. If any one of the functions fails to be traced, it will result in the failure of all functions. The best approach is to filter out the functions that cannot be traced to ensure proper tracking of the functions.Reported-by kernel test robot <[email protected]>Suggested-by Jiri Olsa <[email protected]>Suggested-by Andrii Nakryiko <[email protected]>Signed-off-by Jackie Liu <[email protected]>---
v2->v3: fix 'fscanf' may overflowtools/lib/bpf/libbpf.c | 122 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 109 insertions(+), 13 deletions(-)tools/lib/bpf/libbpf.c +30
-10@@ -10224,6 +10224,12 @@ static const char *tracefs_uprobe_events(void)return use_debugfs () ?DEBUGFS "/uprobe_events" :TRACEFS "/uprobe_events" ; } +static const char *tracefs_available_filter_functions (void ) +{ +return use_debugfs () ?DEBUGFS "/available_filter_functions" : +TRACEFS "/available_filter_functions" ; +} +static void gen_kprobe_legacy_event_name (char *buf,size_t buf_sz,const char *kfunc_name,size_t offset) {@@ -10539,23 +10545,113 @@ struct kprobe_multi_resolve {size_t cnt; }; -static int -resolve_kprobe_multi_cb (unsigned long long sym_addr,char sym_type, -const char *sym_name,void *ctx) +static int qsort_compare_function (const void *a,const void *b) { -struct kprobe_multi_resolve *res = ctx; -int err; +return strcmp (*(const char **)a, *(const char **)b); +} -if (!glob_match (sym_name, res->pattern)) -return 0 ; +static int bsearch_compare_function (const void *a,const void *b) +{ +return strcmp ((const char *)a, *(const char **)b); +} - err =libbpf_ensure_mem ((void **) &res->addrs, &res->cap,sizeof (unsigned long ), - res->cnt +1 ); -if (err) +static int libbpf_available_kallsyms_parse (struct kprobe_multi_resolve *res) +{ +char sym_name[500 ]; +const char *available_functions_file =tracefs_available_filter_functions (); +FILE *f; +int err =0 , ret, i; +struct function_info { +const char **syms; +size_t cap; +size_t cnt; + } infos = {}; + + f =fopen (available_functions_file,"r" ); +if (!f) { + err = -errno; +pr_warn ("failed to open %s\n" , available_functions_file);return err; + }
tools/lib/bpf/libbpf.c struct kprobe_multi_resolve { · line 10583
Quoted 10 lines
tools/lib/bpf/libbpf.c +7
-2- res->addrs[res->cnt++] = (unsigned long ) sym_addr; -return 0 ; +while (true) { +char *name; + + ret =fscanf (f,"%499s%*[^\n]\n" , sym_name); +if (ret ==EOF &&feof (f)) +break ; +
Looks like you fixed up the fclose() issues, sorry about the noise reading email backwards.
bit of a nit...
Its probably worth handling the case where ret == EOF and its not feof(f) that man page claims can happen on read error for example. Might never happen but would be good to distinguish from -EINVAL below?
Quoted 31 lines
tools/lib/bpf/libbpf.c +31
-0+if (ret !=1 ) { +pr_warn ("failed to read available function file entry: %d\n" , + ret); + err = -EINVAL ; + goto cleanup; + } + +if (!glob_match (sym_name, res->pattern)) +continue ; + + err =libbpf_ensure_mem ((void **)&infos.syms, &infos.cap, +sizeof (void *), infos.cnt +1 ); +if (err) + goto cleanup; + + name =strdup (sym_name); +if (!name) { + err = -errno; + goto cleanup; + } + + infos.syms[infos.cnt++] = name; + } +fclose (f); + +/* sort available functions */ +qsort (infos.syms, infos.cnt,sizeof (void *), qsort_compare_function); + + f =fopen ("/proc/kallsyms" ,"r" ); +if (!f) { + err = -errno;
tools/lib/bpf/libbpf.c struct kprobe_multi_resolve { · line 10624
Quoted 10 lines
tools/lib/bpf/libbpf.c +10
-0+pr_warn ("failed to open /proc/kallsyms\n" ); + goto free_infos; + } + +while (true) { +unsigned long long sym_addr; + + ret =fscanf (f,"%llx %*c %499s%*[^\n]\n" , &sym_addr, sym_name); +if (ret ==EOF &&feof (f)) +break ;
Same off chance we get ret == EOF and !feof(f)?
tools/lib/bpf/libbpf.c struct kprobe_multi_resolve { · line 10637
Quoted 13 lines
tools/lib/bpf/libbpf.c +13
-0+ +if (ret !=2 ) { +pr_warn ("failed to read kallsyms entry: %d\n" , ret); + err = -EINVAL ; +break ; + } + +if (!glob_match (sym_name, res->pattern)) +continue ; + +if (!bsearch (&sym_name, infos.syms, infos.cnt,sizeof (void *), + bsearch_compare_function)) +continue ;
I'm wondering if we could get a debug print if the func was skipped? Its not always clear when running many kernels what is going to be skipped and where.
Quoted 33 lines
tools/lib/bpf/libbpf.c +18
-1+ + err =libbpf_ensure_mem ((void **)&res->addrs, &res->cap, +sizeof (unsigned long ), res->cnt +1 ); +if (err) +break ; + + res->addrs[res->cnt++] = (unsigned long ) sym_addr; + } + +cleanup: +fclose (f); +free_infos: +for (i =0 ; i < infos.cnt; i++) +free ((char *)infos.syms[i]); +free (infos.syms); + +return err; } struct bpf_link *@@ -10594,7 +10690,7 @@ bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,return libbpf_err_ptr (-EINVAL );if (pattern) { - err =libbpf_kallsyms_parse (resolve_kprobe_multi_cb, &res); + err =libbpf_available_kallsyms_parse (&res);if (err) goto error;if (!res.cnt) {Signature · 1 line
2.25.1
Quoted 95 lines · John Fastabend
Quoted 79 lines · Jackie Liu
From: Jackie Liu <[email protected]>
When using regular expression matching with "kprobe multi", it scans all the functions under "
/proc/kallsyms" that can be matched. However, not all of them can be traced by kprobe.multi. If any one of the functions fails to be traced, it will result in the failure of all functions. The best approach is to filter out the functions that cannot be traced to ensure proper tracking of the functions.Reported-by kernel test robot <[email protected]>Suggested-by Jiri Olsa <[email protected]>Suggested-by Andrii Nakryiko <[email protected]>Signed-off-by Jackie Liu <[email protected]>---
v2->v3: fix 'fscanf' may overflowtools/lib/bpf/libbpf.c | 122 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 109 insertions(+), 13 deletions(-)tools/lib/bpf/libbpf.c +30
-10@@ -10224,6 +10224,12 @@ static const char *tracefs_uprobe_events(void)return use_debugfs () ?DEBUGFS "/uprobe_events" :TRACEFS "/uprobe_events" ; } +static const char *tracefs_available_filter_functions (void ) +{ +return use_debugfs () ?DEBUGFS "/available_filter_functions" : +TRACEFS "/available_filter_functions" ; +} +static void gen_kprobe_legacy_event_name (char *buf,size_t buf_sz,const char *kfunc_name,size_t offset) {@@ -10539,23 +10545,113 @@ struct kprobe_multi_resolve {size_t cnt; }; -static int -resolve_kprobe_multi_cb (unsigned long long sym_addr,char sym_type, -const char *sym_name,void *ctx) +static int qsort_compare_function (const void *a,const void *b) { -struct kprobe_multi_resolve *res = ctx; -int err; +return strcmp (*(const char **)a, *(const char **)b); +} -if (!glob_match (sym_name, res->pattern)) -return 0 ; +static int bsearch_compare_function (const void *a,const void *b) +{ +return strcmp ((const char *)a, *(const char **)b); +} - err =libbpf_ensure_mem ((void **) &res->addrs, &res->cap,sizeof (unsigned long ), - res->cnt +1 ); -if (err) +static int libbpf_available_kallsyms_parse (struct kprobe_multi_resolve *res) +{ +char sym_name[500 ]; +const char *available_functions_file =tracefs_available_filter_functions (); +FILE *f; +int err =0 , ret, i; +struct function_info { +const char **syms; +size_t cap; +size_t cnt; + } infos = {}; + + f =fopen (available_functions_file,"r" ); +if (!f) { + err = -errno; +pr_warn ("failed to open %s\n" , available_functions_file);return err; + }tools/lib/bpf/libbpf.c struct kprobe_multi_resolve { · line 10583
Quoted 10 lines
tools/lib/bpf/libbpf.c +7
-2- res->addrs[res->cnt++] = (unsigned long ) sym_addr; -return 0 ; +while (true) { +char *name; + + ret =fscanf (f,"%499s%*[^\n]\n" , sym_name); +if (ret ==EOF &&feof (f)) +break ; +Looks like you fixed up the
fclose()issues, sorry about the noise reading email backwards.
Quoted 6 lines
bit of a nit...
Its probably worth handling the case where ret == EOF and its not feof(f) that man page claims can happen on read error for example. Might never happen but would be good to distinguish from
-EINVALbelow?
I think it should not be necessary, we only care about whether we have read the correct data or not, other cases are read failures.
Quoted 54 lines
Quoted 32 lines
tools/lib/bpf/libbpf.c +31
-0+if (ret !=1 ) { +pr_warn ("failed to read available function file entry: %d\n" , + ret); + err = -EINVAL ; + goto cleanup; + } + +if (!glob_match (sym_name, res->pattern)) +continue ; + + err =libbpf_ensure_mem ((void **)&infos.syms, &infos.cap, +sizeof (void *), infos.cnt +1 ); +if (err) + goto cleanup; + + name =strdup (sym_name); +if (!name) { + err = -errno; + goto cleanup; + } + + infos.syms[infos.cnt++] = name; + } +fclose (f); + +/* sort available functions */ +qsort (infos.syms, infos.cnt,sizeof (void *), qsort_compare_function); + + f =fopen ("/proc/kallsyms" ,"r" ); +if (!f) { + err = -errno;tools/lib/bpf/libbpf.c struct kprobe_multi_resolve { · line 10624
Quoted 10 lines
tools/lib/bpf/libbpf.c +10
-0+pr_warn ("failed to open /proc/kallsyms\n" ); + goto free_infos; + } + +while (true) { +unsigned long long sym_addr; + + ret =fscanf (f,"%llx %*c %499s%*[^\n]\n" , &sym_addr, sym_name); +if (ret ==EOF &&feof (f)) +break ;Same off chance we get ret == EOF and !feof(f)?
Quoted 9 lines
tools/lib/bpf/libbpf.c +9
-0+ +if (ret !=2 ) { +pr_warn ("failed to read kallsyms entry: %d\n" , ret); + err = -EINVAL ; +break ; + } + +if (!glob_match (sym_name, res->pattern)) +continue ;
Quoted 9 lines
tools/lib/bpf/libbpf.c struct kprobe_multi_resolve { · line 10637
Quoted 4 lines
tools/lib/bpf/libbpf.c +4
-0+ +if (!bsearch (&sym_name, infos.syms, infos.cnt,sizeof (void *), + bsearch_compare_function)) +continue ;I'm wondering if we could get a debug print if the func was skipped? Its not always clear when running many kernels what is going to be skipped and where.
If there is no match, it will be skipped, and if you add printing, it will become particularly noisy. And print here is not more helpful for debugging, we don't care about skipped functions.
Signature · 1 line
Jackie
Quoted 33 lines
Quoted 33 lines
tools/lib/bpf/libbpf.c +18
-1+ + err =libbpf_ensure_mem ((void **)&res->addrs, &res->cap, +sizeof (unsigned long ), res->cnt +1 ); +if (err) +break ; + + res->addrs[res->cnt++] = (unsigned long ) sym_addr; + } + +cleanup: +fclose (f); +free_infos: +for (i =0 ; i < infos.cnt; i++) +free ((char *)infos.syms[i]); +free (infos.syms); + +return err; } struct bpf_link *@@ -10594,7 +10690,7 @@ bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,return libbpf_err_ptr (-EINVAL );if (pattern) { - err =libbpf_kallsyms_parse (resolve_kprobe_multi_cb, &res); + err =libbpf_available_kallsyms_parse (&res);if (err) goto error;if (!res.cnt) {Signature · 1 line
2.25.1