diff mbox series

[bpf-next,2/6] bpftool: Show probed function in kprobe_multi link info

Message ID 20230602085239.91138-3-laoar.shao@gmail.com (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series bpf: Support ->fill_link_info for kprobe prog | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR fail PR summary
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ${{ matrix.test }} on ${{ matrix.arch }} with ${{ matrix.toolchain_full }}
bpf/vmtest-bpf-next-VM_Test-2 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-3 fail Logs for build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-4 fail Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-5 fail Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-6 fail Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-7 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-8 success Logs for veristat
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for bpf-next, async
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 8 this patch: 8
netdev/cc_maintainers success CCed 13 of 13 maintainers
netdev/build_clang success Errors and warnings before: 8 this patch: 8
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 8 this patch: 8
netdev/checkpatch warning WARNING: line length of 81 exceeds 80 columns
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Yafang Shao June 2, 2023, 8:52 a.m. UTC
Show the already expose kprobe_multi link info in bpftool. The result as
follows,
$ tools/bpf/bpftool/bpftool link show
1: kprobe_multi  prog 5
        func_cnt 4  addrs            symbols
                    ffffffffb4d465b0 schedule_timeout_interruptible
                    ffffffffb4d465f0 schedule_timeout_killable
                    ffffffffb4d46630 schedule_timeout_uninterruptible
                    ffffffffb4d46670 schedule_timeout_idle
        pids trace(8729)

$ tools/bpf/bpftool/bpftool link show -j
[{"id":1,"type":"kprobe_multi","prog_id":5,"func_cnt":4,"addrs":[{"addr":18446744072448402864,"symbol":"schedule_timeout_interruptible"},{"addr":18446744072448402928,"symbol":"schedule_timeout_killable"},{"addr":18446744072448402992,"symbol":"schedule_timeout_uninterruptible"},{"addr":18446744072448403056,"symbol":"schedule_timeout_idle"}],"pids":[{"pid":8729,"comm":"trace"}]}]

Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
---
 tools/bpf/bpftool/link.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 94 insertions(+)

Comments

Alexei Starovoitov June 2, 2023, 8:36 p.m. UTC | #1
On Fri, Jun 02, 2023 at 08:52:35AM +0000, Yafang Shao wrote:
> Show the already expose kprobe_multi link info in bpftool. The result as
> follows,
> $ tools/bpf/bpftool/bpftool link show
> 1: kprobe_multi  prog 5
>         func_cnt 4  addrs            symbols
>                     ffffffffb4d465b0 schedule_timeout_interruptible
>                     ffffffffb4d465f0 schedule_timeout_killable
>                     ffffffffb4d46630 schedule_timeout_uninterruptible
>                     ffffffffb4d46670 schedule_timeout_idle
>         pids trace(8729)
> 
> $ tools/bpf/bpftool/bpftool link show -j
> [{"id":1,"type":"kprobe_multi","prog_id":5,"func_cnt":4,"addrs":[{"addr":18446744072448402864,"symbol":"schedule_timeout_interruptible"},{"addr":18446744072448402928,"symbol":"schedule_timeout_killable"},{"addr":18446744072448402992,"symbol":"schedule_timeout_uninterruptible"},{"addr":18446744072448403056,"symbol":"schedule_timeout_idle"}],"pids":[{"pid":8729,"comm":"trace"}]}]
> 
> Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
> ---
>  tools/bpf/bpftool/link.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 94 insertions(+)
> 
> diff --git a/tools/bpf/bpftool/link.c b/tools/bpf/bpftool/link.c
> index 2d78607..3b00c07 100644
> --- a/tools/bpf/bpftool/link.c
> +++ b/tools/bpf/bpftool/link.c
> @@ -166,6 +166,57 @@ static int get_prog_info(int prog_id, struct bpf_prog_info *info)
>  	return err;
>  }
>  
> +static int cmp_u64(const void *A, const void *B)
> +{
> +	const __u64 *a = A, *b = B;
> +
> +	return *a - *b;
> +}
> +
> +static void kprobe_multi_print_plain(__u64 addr, char *sym, __u32 indent)
> +{
> +	printf("\n\t%*s  %0*llx %s", indent, "", 16, addr, sym);
> +}
> +
> +static void kprobe_multi_print_json(__u64 addr, char *sym)
> +{
> +	jsonw_start_object(json_wtr);
> +	jsonw_uint_field(json_wtr, "addr", addr);
> +	jsonw_string_field(json_wtr, "symbol", sym);
> +	jsonw_end_object(json_wtr);
> +}
> +
> +static void kernel_syms_show(const __u64 *addrs, __u32 cnt, __u32 indent)
> +{
> +	char buff[256], sym[256];
> +	__u64 addr;
> +	int i = 0;
> +	FILE *fp;
> +
> +	fp = fopen("/proc/kallsyms", "r");
> +	if (!fp)
> +		return;
> +
> +	/* Each address is guaranteed to be unique. */
> +	qsort((void *)addrs, cnt, sizeof(__u64), cmp_u64);
> +	/* The addresses in /proc/kallsyms are already sorted. */
> +	while (fgets(buff, sizeof(buff), fp)) {
> +		if (sscanf(buff, "%llx %*c %s", &addr, sym) != 2)
> +			continue;
> +		/* The addr probed by kprobe_multi is always in
> +		 * /proc/kallsyms, so we can ignore some edge cases.
> +		 */
> +		if (addr != addrs[i])
> +			continue;
> +		if (indent)
> +			kprobe_multi_print_plain(addr, sym, indent);
> +		else
> +			kprobe_multi_print_json(addr, sym);
> +		i++;
> +	}
> +	fclose(fp);

There is kernel_syms_load().
Let's reuse it instead of reimplementing kallsysm parsing?
Andrii Nakryiko June 2, 2023, 10:16 p.m. UTC | #2
On Fri, Jun 2, 2023 at 1:52 AM Yafang Shao <laoar.shao@gmail.com> wrote:
>
> Show the already expose kprobe_multi link info in bpftool. The result as
> follows,
> $ tools/bpf/bpftool/bpftool link show
> 1: kprobe_multi  prog 5
>         func_cnt 4  addrs            symbols
>                     ffffffffb4d465b0 schedule_timeout_interruptible
>                     ffffffffb4d465f0 schedule_timeout_killable
>                     ffffffffb4d46630 schedule_timeout_uninterruptible
>                     ffffffffb4d46670 schedule_timeout_idle
>         pids trace(8729)
>
> $ tools/bpf/bpftool/bpftool link show -j
> [{"id":1,"type":"kprobe_multi","prog_id":5,"func_cnt":4,"addrs":[{"addr":18446744072448402864,"symbol":"schedule_timeout_interruptible"},{"addr":18446744072448402928,"symbol":"schedule_timeout_killable"},{"addr":18446744072448402992,"symbol":"schedule_timeout_uninterruptible"},{"addr":18446744072448403056,"symbol":"schedule_timeout_idle"}],"pids":[{"pid":8729,"comm":"trace"}]}]


probably a good idea to also show whether it's retprobe or not?

>
> Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
> ---
>  tools/bpf/bpftool/link.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 94 insertions(+)
>
> diff --git a/tools/bpf/bpftool/link.c b/tools/bpf/bpftool/link.c
> index 2d78607..3b00c07 100644
> --- a/tools/bpf/bpftool/link.c
> +++ b/tools/bpf/bpftool/link.c
> @@ -166,6 +166,57 @@ static int get_prog_info(int prog_id, struct bpf_prog_info *info)
>         return err;
>  }
>
> +static int cmp_u64(const void *A, const void *B)
> +{
> +       const __u64 *a = A, *b = B;
> +
> +       return *a - *b;
> +}
> +
> +static void kprobe_multi_print_plain(__u64 addr, char *sym, __u32 indent)
> +{
> +       printf("\n\t%*s  %0*llx %s", indent, "", 16, addr, sym);
> +}
> +
> +static void kprobe_multi_print_json(__u64 addr, char *sym)
> +{
> +       jsonw_start_object(json_wtr);
> +       jsonw_uint_field(json_wtr, "addr", addr);
> +       jsonw_string_field(json_wtr, "symbol", sym);
> +       jsonw_end_object(json_wtr);
> +}
> +
> +static void kernel_syms_show(const __u64 *addrs, __u32 cnt, __u32 indent)
> +{
> +       char buff[256], sym[256];
> +       __u64 addr;
> +       int i = 0;
> +       FILE *fp;
> +
> +       fp = fopen("/proc/kallsyms", "r");
> +       if (!fp)
> +               return;
> +
> +       /* Each address is guaranteed to be unique. */
> +       qsort((void *)addrs, cnt, sizeof(__u64), cmp_u64);
> +       /* The addresses in /proc/kallsyms are already sorted. */
> +       while (fgets(buff, sizeof(buff), fp)) {
> +               if (sscanf(buff, "%llx %*c %s", &addr, sym) != 2)
> +                       continue;
> +               /* The addr probed by kprobe_multi is always in
> +                * /proc/kallsyms, so we can ignore some edge cases.
> +                */
> +               if (addr != addrs[i])
> +                       continue;
> +               if (indent)
> +                       kprobe_multi_print_plain(addr, sym, indent);
> +               else
> +                       kprobe_multi_print_json(addr, sym);
> +               i++;
> +       }
> +       fclose(fp);
> +}
> +
>  static int show_link_close_json(int fd, struct bpf_link_info *info)
>  {
>         struct bpf_prog_info prog_info;
> @@ -218,6 +269,17 @@ static int show_link_close_json(int fd, struct bpf_link_info *info)
>                 jsonw_uint_field(json_wtr, "map_id",
>                                  info->struct_ops.map_id);
>                 break;
> +       case BPF_LINK_TYPE_KPROBE_MULTI:
> +               const __u64 *addrs;
> +
> +               jsonw_uint_field(json_wtr, "func_cnt", info->kprobe_multi.count);
> +               jsonw_name(json_wtr, "addrs");
> +               jsonw_start_array(json_wtr);
> +               addrs = (const __u64 *)u64_to_ptr(info->kprobe_multi.addrs);
> +               if (info->kprobe_multi.count)
> +                       kernel_syms_show(addrs, info->kprobe_multi.count, 0);
> +               jsonw_end_array(json_wtr);
> +               break;
>         default:
>                 break;
>         }
> @@ -396,6 +458,20 @@ static int show_link_close_plain(int fd, struct bpf_link_info *info)
>         case BPF_LINK_TYPE_NETFILTER:
>                 netfilter_dump_plain(info);
>                 break;
> +       case BPF_LINK_TYPE_KPROBE_MULTI:
> +               __u32 indent, cnt, i;
> +               const __u64 *addrs;
> +
> +               cnt = info->kprobe_multi.count;
> +               if (!cnt)
> +                       break;
> +               printf("\n\tfunc_cnt %d  %-16s %s", cnt, "addrs", "symbols");
> +               for (i = 0; cnt; i++)
> +                       cnt /= 10;
> +               indent = strlen("func_cnt ") + i;
> +               addrs = (const __u64 *)u64_to_ptr(info->kprobe_multi.addrs);
> +               kernel_syms_show(addrs, cnt, indent);
> +               break;
>         default:
>                 break;
>         }
> @@ -417,7 +493,9 @@ static int do_show_link(int fd)
>  {
>         struct bpf_link_info info;
>         __u32 len = sizeof(info);
> +       __u64 *addrs = NULL;
>         char buf[256];
> +       int count;
>         int err;
>
>         memset(&info, 0, sizeof(info));
> @@ -441,12 +519,28 @@ static int do_show_link(int fd)
>                 info.iter.target_name_len = sizeof(buf);
>                 goto again;
>         }
> +       if (info.type == BPF_LINK_TYPE_KPROBE_MULTI &&
> +           !info.kprobe_multi.addrs) {
> +               count = info.kprobe_multi.count;
> +               if (count) {
> +                       addrs = calloc(count, sizeof(__u64));
> +                       if (!addrs) {
> +                               p_err("mem alloc failed");
> +                               close(fd);
> +                               return -1;
> +                       }
> +                       info.kprobe_multi.addrs = (unsigned long)addrs;
> +                       goto again;
> +               }
> +       }
>
>         if (json_output)
>                 show_link_close_json(fd, &info);
>         else
>                 show_link_close_plain(fd, &info);
>
> +       if (addrs)
> +               free(addrs);
>         close(fd);
>         return 0;
>  }
> --
> 1.8.3.1
>
Yafang Shao June 4, 2023, 3:24 a.m. UTC | #3
On Sat, Jun 3, 2023 at 4:37 AM Alexei Starovoitov
<alexei.starovoitov@gmail.com> wrote:
>
> On Fri, Jun 02, 2023 at 08:52:35AM +0000, Yafang Shao wrote:
> > Show the already expose kprobe_multi link info in bpftool. The result as
> > follows,
> > $ tools/bpf/bpftool/bpftool link show
> > 1: kprobe_multi  prog 5
> >         func_cnt 4  addrs            symbols
> >                     ffffffffb4d465b0 schedule_timeout_interruptible
> >                     ffffffffb4d465f0 schedule_timeout_killable
> >                     ffffffffb4d46630 schedule_timeout_uninterruptible
> >                     ffffffffb4d46670 schedule_timeout_idle
> >         pids trace(8729)
> >
> > $ tools/bpf/bpftool/bpftool link show -j
> > [{"id":1,"type":"kprobe_multi","prog_id":5,"func_cnt":4,"addrs":[{"addr":18446744072448402864,"symbol":"schedule_timeout_interruptible"},{"addr":18446744072448402928,"symbol":"schedule_timeout_killable"},{"addr":18446744072448402992,"symbol":"schedule_timeout_uninterruptible"},{"addr":18446744072448403056,"symbol":"schedule_timeout_idle"}],"pids":[{"pid":8729,"comm":"trace"}]}]
> >
> > Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
> > ---
> >  tools/bpf/bpftool/link.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++
> >  1 file changed, 94 insertions(+)
> >
> > diff --git a/tools/bpf/bpftool/link.c b/tools/bpf/bpftool/link.c
> > index 2d78607..3b00c07 100644
> > --- a/tools/bpf/bpftool/link.c
> > +++ b/tools/bpf/bpftool/link.c
> > @@ -166,6 +166,57 @@ static int get_prog_info(int prog_id, struct bpf_prog_info *info)
> >       return err;
> >  }
> >
> > +static int cmp_u64(const void *A, const void *B)
> > +{
> > +     const __u64 *a = A, *b = B;
> > +
> > +     return *a - *b;
> > +}
> > +
> > +static void kprobe_multi_print_plain(__u64 addr, char *sym, __u32 indent)
> > +{
> > +     printf("\n\t%*s  %0*llx %s", indent, "", 16, addr, sym);
> > +}
> > +
> > +static void kprobe_multi_print_json(__u64 addr, char *sym)
> > +{
> > +     jsonw_start_object(json_wtr);
> > +     jsonw_uint_field(json_wtr, "addr", addr);
> > +     jsonw_string_field(json_wtr, "symbol", sym);
> > +     jsonw_end_object(json_wtr);
> > +}
> > +
> > +static void kernel_syms_show(const __u64 *addrs, __u32 cnt, __u32 indent)
> > +{
> > +     char buff[256], sym[256];
> > +     __u64 addr;
> > +     int i = 0;
> > +     FILE *fp;
> > +
> > +     fp = fopen("/proc/kallsyms", "r");
> > +     if (!fp)
> > +             return;
> > +
> > +     /* Each address is guaranteed to be unique. */
> > +     qsort((void *)addrs, cnt, sizeof(__u64), cmp_u64);
> > +     /* The addresses in /proc/kallsyms are already sorted. */
> > +     while (fgets(buff, sizeof(buff), fp)) {
> > +             if (sscanf(buff, "%llx %*c %s", &addr, sym) != 2)
> > +                     continue;
> > +             /* The addr probed by kprobe_multi is always in
> > +              * /proc/kallsyms, so we can ignore some edge cases.
> > +              */
> > +             if (addr != addrs[i])
> > +                     continue;
> > +             if (indent)
> > +                     kprobe_multi_print_plain(addr, sym, indent);
> > +             else
> > +                     kprobe_multi_print_json(addr, sym);
> > +             i++;
> > +     }
> > +     fclose(fp);
>
> There is kernel_syms_load().
> Let's reuse it instead of reimplementing kallsysm parsing?

I will think about it. Thanks for your suggestion.
Yafang Shao June 4, 2023, 3:26 a.m. UTC | #4
On Sat, Jun 3, 2023 at 6:16 AM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Fri, Jun 2, 2023 at 1:52 AM Yafang Shao <laoar.shao@gmail.com> wrote:
> >
> > Show the already expose kprobe_multi link info in bpftool. The result as
> > follows,
> > $ tools/bpf/bpftool/bpftool link show
> > 1: kprobe_multi  prog 5
> >         func_cnt 4  addrs            symbols
> >                     ffffffffb4d465b0 schedule_timeout_interruptible
> >                     ffffffffb4d465f0 schedule_timeout_killable
> >                     ffffffffb4d46630 schedule_timeout_uninterruptible
> >                     ffffffffb4d46670 schedule_timeout_idle
> >         pids trace(8729)
> >
> > $ tools/bpf/bpftool/bpftool link show -j
> > [{"id":1,"type":"kprobe_multi","prog_id":5,"func_cnt":4,"addrs":[{"addr":18446744072448402864,"symbol":"schedule_timeout_interruptible"},{"addr":18446744072448402928,"symbol":"schedule_timeout_killable"},{"addr":18446744072448402992,"symbol":"schedule_timeout_uninterruptible"},{"addr":18446744072448403056,"symbol":"schedule_timeout_idle"}],"pids":[{"pid":8729,"comm":"trace"}]}]
>
>
> probably a good idea to also show whether it's retprobe or not?

Good point. Will add it.
diff mbox series

Patch

diff --git a/tools/bpf/bpftool/link.c b/tools/bpf/bpftool/link.c
index 2d78607..3b00c07 100644
--- a/tools/bpf/bpftool/link.c
+++ b/tools/bpf/bpftool/link.c
@@ -166,6 +166,57 @@  static int get_prog_info(int prog_id, struct bpf_prog_info *info)
 	return err;
 }
 
+static int cmp_u64(const void *A, const void *B)
+{
+	const __u64 *a = A, *b = B;
+
+	return *a - *b;
+}
+
+static void kprobe_multi_print_plain(__u64 addr, char *sym, __u32 indent)
+{
+	printf("\n\t%*s  %0*llx %s", indent, "", 16, addr, sym);
+}
+
+static void kprobe_multi_print_json(__u64 addr, char *sym)
+{
+	jsonw_start_object(json_wtr);
+	jsonw_uint_field(json_wtr, "addr", addr);
+	jsonw_string_field(json_wtr, "symbol", sym);
+	jsonw_end_object(json_wtr);
+}
+
+static void kernel_syms_show(const __u64 *addrs, __u32 cnt, __u32 indent)
+{
+	char buff[256], sym[256];
+	__u64 addr;
+	int i = 0;
+	FILE *fp;
+
+	fp = fopen("/proc/kallsyms", "r");
+	if (!fp)
+		return;
+
+	/* Each address is guaranteed to be unique. */
+	qsort((void *)addrs, cnt, sizeof(__u64), cmp_u64);
+	/* The addresses in /proc/kallsyms are already sorted. */
+	while (fgets(buff, sizeof(buff), fp)) {
+		if (sscanf(buff, "%llx %*c %s", &addr, sym) != 2)
+			continue;
+		/* The addr probed by kprobe_multi is always in
+		 * /proc/kallsyms, so we can ignore some edge cases.
+		 */
+		if (addr != addrs[i])
+			continue;
+		if (indent)
+			kprobe_multi_print_plain(addr, sym, indent);
+		else
+			kprobe_multi_print_json(addr, sym);
+		i++;
+	}
+	fclose(fp);
+}
+
 static int show_link_close_json(int fd, struct bpf_link_info *info)
 {
 	struct bpf_prog_info prog_info;
@@ -218,6 +269,17 @@  static int show_link_close_json(int fd, struct bpf_link_info *info)
 		jsonw_uint_field(json_wtr, "map_id",
 				 info->struct_ops.map_id);
 		break;
+	case BPF_LINK_TYPE_KPROBE_MULTI:
+		const __u64 *addrs;
+
+		jsonw_uint_field(json_wtr, "func_cnt", info->kprobe_multi.count);
+		jsonw_name(json_wtr, "addrs");
+		jsonw_start_array(json_wtr);
+		addrs = (const __u64 *)u64_to_ptr(info->kprobe_multi.addrs);
+		if (info->kprobe_multi.count)
+			kernel_syms_show(addrs, info->kprobe_multi.count, 0);
+		jsonw_end_array(json_wtr);
+		break;
 	default:
 		break;
 	}
@@ -396,6 +458,20 @@  static int show_link_close_plain(int fd, struct bpf_link_info *info)
 	case BPF_LINK_TYPE_NETFILTER:
 		netfilter_dump_plain(info);
 		break;
+	case BPF_LINK_TYPE_KPROBE_MULTI:
+		__u32 indent, cnt, i;
+		const __u64 *addrs;
+
+		cnt = info->kprobe_multi.count;
+		if (!cnt)
+			break;
+		printf("\n\tfunc_cnt %d  %-16s %s", cnt, "addrs", "symbols");
+		for (i = 0; cnt; i++)
+			cnt /= 10;
+		indent = strlen("func_cnt ") + i;
+		addrs = (const __u64 *)u64_to_ptr(info->kprobe_multi.addrs);
+		kernel_syms_show(addrs, cnt, indent);
+		break;
 	default:
 		break;
 	}
@@ -417,7 +493,9 @@  static int do_show_link(int fd)
 {
 	struct bpf_link_info info;
 	__u32 len = sizeof(info);
+	__u64 *addrs = NULL;
 	char buf[256];
+	int count;
 	int err;
 
 	memset(&info, 0, sizeof(info));
@@ -441,12 +519,28 @@  static int do_show_link(int fd)
 		info.iter.target_name_len = sizeof(buf);
 		goto again;
 	}
+	if (info.type == BPF_LINK_TYPE_KPROBE_MULTI &&
+	    !info.kprobe_multi.addrs) {
+		count = info.kprobe_multi.count;
+		if (count) {
+			addrs = calloc(count, sizeof(__u64));
+			if (!addrs) {
+				p_err("mem alloc failed");
+				close(fd);
+				return -1;
+			}
+			info.kprobe_multi.addrs = (unsigned long)addrs;
+			goto again;
+		}
+	}
 
 	if (json_output)
 		show_link_close_json(fd, &info);
 	else
 		show_link_close_plain(fd, &info);
 
+	if (addrs)
+		free(addrs);
 	close(fd);
 	return 0;
 }