diff mbox series

[PATCHv4,bpf-next,6/8] libbpf: Add bpf_program__attach_kprobe_opts function

Message ID 20210714094400.396467-7-jolsa@kernel.org
State New
Headers show
Series bpf, x86: Add bpf_get_func_ip helper | expand

Commit Message

Jiri Olsa July 14, 2021, 9:43 a.m. UTC
Adding bpf_program__attach_kprobe_opts that does the same
as bpf_program__attach_kprobe, but takes opts argument.

Currently opts struct holds just retprobe bool, but we will
add new field in following patch.

The function is not exported, so there's no need to add
size to the struct bpf_program_attach_kprobe_opts for now.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
 tools/lib/bpf/libbpf.c | 34 +++++++++++++++++++++++++---------
 1 file changed, 25 insertions(+), 9 deletions(-)

Comments

Andrii Nakryiko July 17, 2021, 1:41 a.m. UTC | #1
On Wed, Jul 14, 2021 at 2:45 AM Jiri Olsa <jolsa@redhat.com> wrote:
>

> Adding bpf_program__attach_kprobe_opts that does the same

> as bpf_program__attach_kprobe, but takes opts argument.

>

> Currently opts struct holds just retprobe bool, but we will

> add new field in following patch.

>

> The function is not exported, so there's no need to add

> size to the struct bpf_program_attach_kprobe_opts for now.


Why not exported? Please use a proper _opts struct just like others
(e.g., bpf_object_open_opts) and add is as a public API, it's a useful
addition. We are going to have a similar structure for attach_uprobe,
btw. Please send a follow up patch.

>

> Signed-off-by: Jiri Olsa <jolsa@kernel.org>

> ---

>  tools/lib/bpf/libbpf.c | 34 +++++++++++++++++++++++++---------

>  1 file changed, 25 insertions(+), 9 deletions(-)

>

> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c

> index 88b99401040c..d93a6f9408d1 100644

> --- a/tools/lib/bpf/libbpf.c

> +++ b/tools/lib/bpf/libbpf.c

> @@ -10346,19 +10346,24 @@ static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name,

>         return pfd;

>  }

>

> -struct bpf_link *bpf_program__attach_kprobe(struct bpf_program *prog,

> -                                           bool retprobe,

> -                                           const char *func_name)

> +struct bpf_program_attach_kprobe_opts {


when you make it part of libbpf API, let's call it something shorter,
like bpf_kprobe_opts, maybe? And later we'll have bpf_uprobe_opts for
uprobes. Short and unambiguous.

> +       bool retprobe;

> +};

> +

> +static struct bpf_link*

> +bpf_program__attach_kprobe_opts(struct bpf_program *prog,

> +                               const char *func_name,

> +                               struct bpf_program_attach_kprobe_opts *opts)

>  {

>         char errmsg[STRERR_BUFSIZE];

>         struct bpf_link *link;

>         int pfd, err;

>


[...]
Jiri Olsa July 18, 2021, 7:32 p.m. UTC | #2
On Fri, Jul 16, 2021 at 06:41:59PM -0700, Andrii Nakryiko wrote:
> On Wed, Jul 14, 2021 at 2:45 AM Jiri Olsa <jolsa@redhat.com> wrote:

> >

> > Adding bpf_program__attach_kprobe_opts that does the same

> > as bpf_program__attach_kprobe, but takes opts argument.

> >

> > Currently opts struct holds just retprobe bool, but we will

> > add new field in following patch.

> >

> > The function is not exported, so there's no need to add

> > size to the struct bpf_program_attach_kprobe_opts for now.

> 

> Why not exported? Please use a proper _opts struct just like others

> (e.g., bpf_object_open_opts) and add is as a public API, it's a useful

> addition. We are going to have a similar structure for attach_uprobe,

> btw. Please send a follow up patch.


there's no outside user.. ok

> 

> >

> > Signed-off-by: Jiri Olsa <jolsa@kernel.org>

> > ---

> >  tools/lib/bpf/libbpf.c | 34 +++++++++++++++++++++++++---------

> >  1 file changed, 25 insertions(+), 9 deletions(-)

> >

> > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c

> > index 88b99401040c..d93a6f9408d1 100644

> > --- a/tools/lib/bpf/libbpf.c

> > +++ b/tools/lib/bpf/libbpf.c

> > @@ -10346,19 +10346,24 @@ static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name,

> >         return pfd;

> >  }

> >

> > -struct bpf_link *bpf_program__attach_kprobe(struct bpf_program *prog,

> > -                                           bool retprobe,

> > -                                           const char *func_name)

> > +struct bpf_program_attach_kprobe_opts {

> 

> when you make it part of libbpf API, let's call it something shorter,

> like bpf_kprobe_opts, maybe? And later we'll have bpf_uprobe_opts for

> uprobes. Short and unambiguous.


ok

jirka

> 

> > +       bool retprobe;

> > +};

> > +

> > +static struct bpf_link*

> > +bpf_program__attach_kprobe_opts(struct bpf_program *prog,

> > +                               const char *func_name,

> > +                               struct bpf_program_attach_kprobe_opts *opts)

> >  {

> >         char errmsg[STRERR_BUFSIZE];

> >         struct bpf_link *link;

> >         int pfd, err;

> >

> 

> [...]

>
Andrii Nakryiko July 18, 2021, 9:30 p.m. UTC | #3
On Sun, Jul 18, 2021 at 12:32 PM Jiri Olsa <jolsa@redhat.com> wrote:
>

> On Fri, Jul 16, 2021 at 06:41:59PM -0700, Andrii Nakryiko wrote:

> > On Wed, Jul 14, 2021 at 2:45 AM Jiri Olsa <jolsa@redhat.com> wrote:

> > >

> > > Adding bpf_program__attach_kprobe_opts that does the same

> > > as bpf_program__attach_kprobe, but takes opts argument.

> > >

> > > Currently opts struct holds just retprobe bool, but we will

> > > add new field in following patch.

> > >

> > > The function is not exported, so there's no need to add

> > > size to the struct bpf_program_attach_kprobe_opts for now.

> >

> > Why not exported? Please use a proper _opts struct just like others

> > (e.g., bpf_object_open_opts) and add is as a public API, it's a useful

> > addition. We are going to have a similar structure for attach_uprobe,

> > btw. Please send a follow up patch.

>

> there's no outside user.. ok


because there is no API :) I've seen people asking about the ability
to attach to kprobe+offset in some PRs.

>

> >

> > >

> > > Signed-off-by: Jiri Olsa <jolsa@kernel.org>

> > > ---

> > >  tools/lib/bpf/libbpf.c | 34 +++++++++++++++++++++++++---------

> > >  1 file changed, 25 insertions(+), 9 deletions(-)

> > >

> > > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c

> > > index 88b99401040c..d93a6f9408d1 100644

> > > --- a/tools/lib/bpf/libbpf.c

> > > +++ b/tools/lib/bpf/libbpf.c

> > > @@ -10346,19 +10346,24 @@ static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name,

> > >         return pfd;

> > >  }

> > >

> > > -struct bpf_link *bpf_program__attach_kprobe(struct bpf_program *prog,

> > > -                                           bool retprobe,

> > > -                                           const char *func_name)

> > > +struct bpf_program_attach_kprobe_opts {

> >

> > when you make it part of libbpf API, let's call it something shorter,

> > like bpf_kprobe_opts, maybe? And later we'll have bpf_uprobe_opts for

> > uprobes. Short and unambiguous.

>

> ok

>

> jirka

>

> >

> > > +       bool retprobe;

> > > +};

> > > +

> > > +static struct bpf_link*

> > > +bpf_program__attach_kprobe_opts(struct bpf_program *prog,

> > > +                               const char *func_name,

> > > +                               struct bpf_program_attach_kprobe_opts *opts)

> > >  {

> > >         char errmsg[STRERR_BUFSIZE];

> > >         struct bpf_link *link;

> > >         int pfd, err;

> > >

> >

> > [...]

> >

>
diff mbox series

Patch

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 88b99401040c..d93a6f9408d1 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -10346,19 +10346,24 @@  static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name,
 	return pfd;
 }
 
-struct bpf_link *bpf_program__attach_kprobe(struct bpf_program *prog,
-					    bool retprobe,
-					    const char *func_name)
+struct bpf_program_attach_kprobe_opts {
+	bool retprobe;
+};
+
+static struct bpf_link*
+bpf_program__attach_kprobe_opts(struct bpf_program *prog,
+				const char *func_name,
+				struct bpf_program_attach_kprobe_opts *opts)
 {
 	char errmsg[STRERR_BUFSIZE];
 	struct bpf_link *link;
 	int pfd, err;
 
-	pfd = perf_event_open_probe(false /* uprobe */, retprobe, func_name,
+	pfd = perf_event_open_probe(false /* uprobe */, opts->retprobe, func_name,
 				    0 /* offset */, -1 /* pid */);
 	if (pfd < 0) {
 		pr_warn("prog '%s': failed to create %s '%s' perf event: %s\n",
-			prog->name, retprobe ? "kretprobe" : "kprobe", func_name,
+			prog->name, opts->retprobe ? "kretprobe" : "kprobe", func_name,
 			libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
 		return libbpf_err_ptr(pfd);
 	}
@@ -10367,23 +10372,34 @@  struct bpf_link *bpf_program__attach_kprobe(struct bpf_program *prog,
 	if (err) {
 		close(pfd);
 		pr_warn("prog '%s': failed to attach to %s '%s': %s\n",
-			prog->name, retprobe ? "kretprobe" : "kprobe", func_name,
+			prog->name, opts->retprobe ? "kretprobe" : "kprobe", func_name,
 			libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
 		return libbpf_err_ptr(err);
 	}
 	return link;
 }
 
+struct bpf_link *bpf_program__attach_kprobe(struct bpf_program *prog,
+					    bool retprobe,
+					    const char *func_name)
+{
+	struct bpf_program_attach_kprobe_opts opts = {
+		.retprobe = retprobe,
+	};
+
+	return bpf_program__attach_kprobe_opts(prog, func_name, &opts);
+}
+
 static struct bpf_link *attach_kprobe(const struct bpf_sec_def *sec,
 				      struct bpf_program *prog)
 {
+	struct bpf_program_attach_kprobe_opts opts;
 	const char *func_name;
-	bool retprobe;
 
 	func_name = prog->sec_name + sec->len;
-	retprobe = strcmp(sec->sec, "kretprobe/") == 0;
+	opts.retprobe = strcmp(sec->sec, "kretprobe/") == 0;
 
-	return bpf_program__attach_kprobe(prog, retprobe, func_name);
+	return bpf_program__attach_kprobe_opts(prog, func_name, &opts);
 }
 
 struct bpf_link *bpf_program__attach_uprobe(struct bpf_program *prog,