bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andrii Nakryiko <andrii.nakryiko@gmail.com>
To: Jiri Olsa <jolsa@kernel.org>
Cc: Arnaldo Carvalho de Melo <acme@kernel.org>,
	dwarves@vger.kernel.org, bpf <bpf@vger.kernel.org>,
	Alexei Starovoitov <ast@kernel.org>,
	Andrii Nakryiko <andriin@fb.com>, Yonghong Song <yhs@fb.com>,
	Hao Luo <haoluo@google.com>
Subject: Re: [PATCH 1/2] btf_encoder: Factor filter_functions function
Date: Thu, 26 Nov 2020 20:05:11 -0800	[thread overview]
Message-ID: <CAEf4BzbjTYevuOU7L0LoT0wL2Jb4fnb5LRXEwo_V52npGgvd8Q@mail.gmail.com> (raw)
In-Reply-To: <20201124161919.2152187-2-jolsa@kernel.org>

On Tue, Nov 24, 2020 at 8:22 AM Jiri Olsa <jolsa@kernel.org> wrote:
>
> Reorder the filter_functions function so we can add
> processing of kernel modules in following patch.
>
> There's no functional change intended.
>
> Signed-off-by: Jiri Olsa <jolsa@kernel.org>
> ---
>  btf_encoder.c | 57 +++++++++++++++++++++++++++++++++------------------
>  1 file changed, 37 insertions(+), 20 deletions(-)
>
> diff --git a/btf_encoder.c b/btf_encoder.c
> index c40f059580da..467c4657b2c0 100644
> --- a/btf_encoder.c
> +++ b/btf_encoder.c
> @@ -101,14 +101,17 @@ static int addrs_cmp(const void *_a, const void *_b)
>         return *a < *b ? -1 : 1;
>  }
>
> -static int filter_functions(struct btf_elf *btfe, struct funcs_layout *fl)
> +static int get_vmlinux_addrs(struct btf_elf *btfe, struct funcs_layout *fl,
> +                            unsigned long **paddrs, unsigned long *pcount)
>  {
> -       unsigned long *addrs, count, offset, i;
> -       int functions_valid = 0;
> +       unsigned long *addrs, count, offset;
>         Elf_Data *data;
>         GElf_Shdr shdr;
>         Elf_Scn *sec;
>
> +       if (!fl->mcount_start || !fl->mcount_stop)
> +               return 0;
> +

probably better to explicitly assign paddrs and pcount to NULL and 0 here

>         /*
>          * Find mcount addressed marked by __start_mcount_loc
>          * and __stop_mcount_loc symbols and load them into
> @@ -138,7 +141,32 @@ static int filter_functions(struct btf_elf *btfe, struct funcs_layout *fl)
>         }
>
>         memcpy(addrs, data->d_buf + offset, count * sizeof(addrs[0]));
> +       *paddrs = addrs;
> +       *pcount = count;
> +       return 0;
> +}
> +
> +static int setup_functions(struct btf_elf *btfe, struct funcs_layout *fl)
> +{
> +       unsigned long *addrs = NULL, count, i;
> +       int functions_valid = 0;
> +
> +       /*
> +        * Check if we are processing vmlinux image and
> +        * get mcount data if it's detected.
> +        */
> +       if (get_vmlinux_addrs(btfe, fl, &addrs, &count))
> +               return -1;
> +
> +       if (!addrs) {
> +               if (btf_elf__verbose)
> +                       printf("ftrace symbols not detected, falling back to DWARF data\n");
> +               delete_functions();
> +               return 0;
> +       }
> +
>         qsort(addrs, count, sizeof(addrs[0]), addrs_cmp);
> +       qsort(functions, functions_cnt, sizeof(functions[0]), functions_cmp);
>
>         /*
>          * Let's got through all collected functions and filter
> @@ -162,6 +190,9 @@ static int filter_functions(struct btf_elf *btfe, struct funcs_layout *fl)
>
>         functions_cnt = functions_valid;
>         free(addrs);
> +
> +       if (btf_elf__verbose)
> +               printf("Found %d functions!\n", functions_cnt);
>         return 0;
>  }
>
> @@ -470,11 +501,6 @@ static void collect_symbol(GElf_Sym *sym, struct funcs_layout *fl)
>                 fl->mcount_stop = sym->st_value;
>  }
>
> -static int has_all_symbols(struct funcs_layout *fl)
> -{
> -       return fl->mcount_start && fl->mcount_stop;
> -}
> -
>  static int collect_symbols(struct btf_elf *btfe, bool collect_percpu_vars)
>  {
>         struct funcs_layout fl = { };
> @@ -501,18 +527,9 @@ static int collect_symbols(struct btf_elf *btfe, bool collect_percpu_vars)
>                         printf("Found %d per-CPU variables!\n", percpu_var_cnt);
>         }
>
> -       if (functions_cnt && has_all_symbols(&fl)) {
> -               qsort(functions, functions_cnt, sizeof(functions[0]), functions_cmp);
> -               if (filter_functions(btfe, &fl)) {
> -                       fprintf(stderr, "Failed to filter dwarf functions\n");
> -                       return -1;
> -               }
> -               if (btf_elf__verbose)
> -                       printf("Found %d functions!\n", functions_cnt);
> -       } else {
> -               if (btf_elf__verbose)
> -                       printf("ftrace symbols not detected, falling back to DWARF data\n");
> -               delete_functions();
> +       if (functions_cnt && setup_functions(btfe, &fl)) {
> +               fprintf(stderr, "Failed to filter dwarf functions\n");

DWARF

> +               return -1;
>         }
>
>         return 0;
> --
> 2.26.2
>

  reply	other threads:[~2020-11-27  4:05 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-24 16:19 [RFC 0/2] btf_encoder: Detect kernel modules Jiri Olsa
2020-11-24 16:19 ` [PATCH 1/2] btf_encoder: Factor filter_functions function Jiri Olsa
2020-11-27  4:05   ` Andrii Nakryiko [this message]
2020-11-27 17:21     ` Jiri Olsa
2020-11-24 16:19 ` [PATCH 2/2] btf_encoder: Detect kernel module ftrace addresses Jiri Olsa
2020-11-27  4:18   ` Andrii Nakryiko
2020-11-27 17:40     ` Jiri Olsa
2020-11-27 20:39       ` Arnaldo Carvalho de Melo

Reply instructions:

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

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

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

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

  git send-email \
    --in-reply-to=CAEf4BzbjTYevuOU7L0LoT0wL2Jb4fnb5LRXEwo_V52npGgvd8Q@mail.gmail.com \
    --to=andrii.nakryiko@gmail.com \
    --cc=acme@kernel.org \
    --cc=andriin@fb.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=dwarves@vger.kernel.org \
    --cc=haoluo@google.com \
    --cc=jolsa@kernel.org \
    --cc=yhs@fb.com \
    /path/to/YOUR_REPLY

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

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