bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jiri Olsa <jolsa@redhat.com>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andriin@fb.com>,
	Networking <netdev@vger.kernel.org>, bpf <bpf@vger.kernel.org>,
	Martin KaFai Lau <kafai@fb.com>, Song Liu <songliubraving@fb.com>,
	Yonghong Song <yhs@fb.com>,
	John Fastabend <john.fastabend@gmail.com>,
	KP Singh <kpsingh@chromium.org>,
	Masami Hiramatsu <mhiramat@kernel.org>,
	Alan Maguire <alan.maguire@oracle.com>
Subject: Re: [PATCHv4 bpf-next 7/8] libbpf: Allow specification of "kprobe/function+offset"
Date: Sun, 18 Jul 2021 21:32:54 +0200	[thread overview]
Message-ID: <YPSB5ot0iK57kkA6@krava> (raw)
In-Reply-To: <CAEf4BzYELMgTv_RhW7qWNgOYc_mCyh8-VX0FUYabi_TU3OiGKw@mail.gmail.com>

On Fri, Jul 16, 2021 at 06:42:05PM -0700, Andrii Nakryiko wrote:
> On Wed, Jul 14, 2021 at 2:45 AM Jiri Olsa <jolsa@redhat.com> wrote:
> >
> > From: Alan Maguire <alan.maguire@oracle.com>
> >
> > kprobes can be placed on most instructions in a function, not
> > just entry, and ftrace and bpftrace support the function+offset
> > notification for probe placement.  Adding parsing of func_name
> > into func+offset to bpf_program__attach_kprobe() allows the
> > user to specify
> >
> > SEC("kprobe/bpf_fentry_test5+0x6")
> >
> > ...for example, and the offset can be passed to perf_event_open_probe()
> > to support kprobe attachment.
> >
> > [jolsa: changed original code to use bpf_program__attach_kprobe_opts
> > and use dynamic allocation in sscanf]
> >
> > Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
> > Signed-off-by: Jiri Olsa <jolsa@kernel.org>
> > ---
> >  tools/lib/bpf/libbpf.c | 24 ++++++++++++++++++++++--
> >  1 file changed, 22 insertions(+), 2 deletions(-)
> >
> > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> > index d93a6f9408d1..abe6d4842bb0 100644
> > --- a/tools/lib/bpf/libbpf.c
> > +++ b/tools/lib/bpf/libbpf.c
> > @@ -10348,6 +10348,7 @@ static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name,
> >
> >  struct bpf_program_attach_kprobe_opts {
> >         bool retprobe;
> > +       unsigned long offset;
> >  };
> >
> >  static struct bpf_link*
> > @@ -10360,7 +10361,7 @@ bpf_program__attach_kprobe_opts(struct bpf_program *prog,
> >         int pfd, err;
> >
> >         pfd = perf_event_open_probe(false /* uprobe */, opts->retprobe, func_name,
> > -                                   0 /* offset */, -1 /* pid */);
> > +                                   opts->offset, -1 /* pid */);
> >         if (pfd < 0) {
> >                 pr_warn("prog '%s': failed to create %s '%s' perf event: %s\n",
> >                         prog->name, opts->retprobe ? "kretprobe" : "kprobe", func_name,
> > @@ -10394,12 +10395,31 @@ static struct bpf_link *attach_kprobe(const struct bpf_sec_def *sec,
> >                                       struct bpf_program *prog)
> >  {
> >         struct bpf_program_attach_kprobe_opts opts;
> > +       unsigned long offset = 0;
> > +       struct bpf_link *link;
> >         const char *func_name;
> > +       char *func;
> > +       int n, err;
> >
> >         func_name = prog->sec_name + sec->len;
> >         opts.retprobe = strcmp(sec->sec, "kretprobe/") == 0;
> >
> > -       return bpf_program__attach_kprobe_opts(prog, func_name, &opts);
> > +       n = sscanf(func_name, "%m[a-zA-Z0-9_.]+%lx", &func, &offset);
> 
> could have used %li here to support both +0xabc and +123 forms

ok

> 
> > +       if (n < 1) {
> > +               err = -EINVAL;
> > +               pr_warn("kprobe name is invalid: %s\n", func_name);
> > +               return libbpf_err_ptr(err);
> > +       }
> > +       if (opts.retprobe && offset != 0) {
> > +               err = -EINVAL;
> 
> leaking func here

ugh.. thanks

jirka

> 
> 
> > +               pr_warn("kretprobes do not support offset specification\n");
> > +               return libbpf_err_ptr(err);
> > +       }
> > +
> > +       opts.offset = offset;
> > +       link = bpf_program__attach_kprobe_opts(prog, func, &opts);
> > +       free(func);
> > +       return link;
> >  }
> >
> >  struct bpf_link *bpf_program__attach_uprobe(struct bpf_program *prog,
> > --
> > 2.31.1
> >
> 


  reply	other threads:[~2021-07-18 19:33 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-14  9:43 [PATCHv4 bpf-next 0/8] bpf, x86: Add bpf_get_func_ip helper Jiri Olsa
2021-07-14  9:43 ` [PATCHv4 bpf-next 1/8] bpf, x86: Store caller's ip in trampoline stack Jiri Olsa
2021-07-14  9:43 ` [PATCHv4 bpf-next 2/8] bpf: Enable BPF_TRAMP_F_IP_ARG for trampolines with call_get_func_ip Jiri Olsa
2021-07-14  9:43 ` [PATCHv4 bpf-next 3/8] bpf: Add bpf_get_func_ip helper for tracing programs Jiri Olsa
2021-07-16  1:09   ` Alexei Starovoitov
2021-07-18 19:30     ` Jiri Olsa
2021-07-14  9:43 ` [PATCHv4 bpf-next 4/8] bpf: Add bpf_get_func_ip helper for kprobe programs Jiri Olsa
2021-07-14  9:43 ` [PATCHv4 bpf-next 5/8] selftests/bpf: Add test for bpf_get_func_ip helper Jiri Olsa
2021-07-14  9:43 ` [PATCHv4 bpf-next 6/8] libbpf: Add bpf_program__attach_kprobe_opts function Jiri Olsa
2021-07-17  1:41   ` Andrii Nakryiko
2021-07-18 19:32     ` Jiri Olsa
2021-07-18 21:30       ` Andrii Nakryiko
2021-07-14  9:43 ` [PATCHv4 bpf-next 7/8] libbpf: Allow specification of "kprobe/function+offset" Jiri Olsa
2021-07-17  1:42   ` Andrii Nakryiko
2021-07-18 19:32     ` Jiri Olsa [this message]
2021-07-14  9:44 ` [PATCHv4 bpf-next 8/8] selftests/bpf: Add test for bpf_get_func_ip in kprobe+offset probe Jiri Olsa

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=YPSB5ot0iK57kkA6@krava \
    --to=jolsa@redhat.com \
    --cc=alan.maguire@oracle.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=andriin@fb.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=john.fastabend@gmail.com \
    --cc=kafai@fb.com \
    --cc=kpsingh@chromium.org \
    --cc=mhiramat@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=songliubraving@fb.com \
    --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).