Subject: [PATCH perf/core 2/2] perf-probe: Check the orphaned -x option

To avoid probing in unintended binary, the orphaned -x option
must be checked and warned.

Without this patch, following command sets up the probe in
the kernel.
-----
# perf probe -a strcpy -x ./perf
Added new event:
probe:strcpy (on strcpy)

You can now use it in all perf tools, such as:

perf record -e probe:strcpy -aR sleep 1
-----

But in this case, it seems that the user may want to probe
in the perf binary. With this patch, perf-probe correctly
handles the orphaned -x.
-----
# perf probe -a strcpy -x ./perf
Error: -x/-m must follow the probe definitions.
...
-----

Reported-by: Jiri Olsa <[email protected]>
Signed-off-by: Masami Hiramatsu <[email protected]>
---
tools/perf/builtin-probe.c | 10 ++++++++++
1 file changed, 10 insertions(+)

diff --git a/tools/perf/builtin-probe.c b/tools/perf/builtin-probe.c
index 2df23e1..f7b1af6 100644
--- a/tools/perf/builtin-probe.c
+++ b/tools/perf/builtin-probe.c
@@ -56,6 +56,7 @@ static struct {
bool mod_events;
bool uprobes;
bool quiet;
+ bool target_used;
int nevents;
struct perf_probe_event events[MAX_PROBES];
struct strlist *dellist;
@@ -82,6 +83,7 @@ static int parse_probe_event(const char *str)
pev->target = strdup(params.target);
if (!pev->target)
return -ENOMEM;
+ params.target_used = true;
}

/* Parse a perf-probe command into event */
@@ -107,6 +109,7 @@ static int set_target(const char *ptr)
params.target = strdup(ptr);
if (!params.target)
return -ENOMEM;
+ params.target_used = false;

found = 1;
buf = ptr + (strlen(ptr) - 3);
@@ -207,6 +210,7 @@ static int opt_set_target(const struct option *opt, const char *str,
}
free(params.target);
params.target = tmp;
+ params.target_used = false;
ret = 0;
}

@@ -491,6 +495,12 @@ __cmd_probe(int argc, const char **argv, const char *prefix __maybe_unused)
}

if (params.nevents) {
+ /* Ensure the last given target is used */
+ if (params.target && !params.target_used) {
+ pr_warning(" Error: -x/-m must follow the probe definitions.\n");
+ usage_with_options(probe_usage, options);
+ }
+
ret = add_perf_probe_events(params.events, params.nevents,
params.max_probe_points,
params.force_add);


2015-04-01 11:11:42

by Jiri Olsa

[permalink] [raw]
Subject: Re: [PATCH perf/core 2/2] perf-probe: Check the orphaned -x option

On Wed, Apr 01, 2015 at 07:25:42PM +0900, Masami Hiramatsu wrote:
> To avoid probing in unintended binary, the orphaned -x option
> must be checked and warned.
>
> Without this patch, following command sets up the probe in
> the kernel.
> -----
> # perf probe -a strcpy -x ./perf
> Added new event:
> probe:strcpy (on strcpy)
>
> You can now use it in all perf tools, such as:
>
> perf record -e probe:strcpy -aR sleep 1
> -----
>
> But in this case, it seems that the user may want to probe
> in the perf binary. With this patch, perf-probe correctly
> handles the orphaned -x.
> -----
> # perf probe -a strcpy -x ./perf
> Error: -x/-m must follow the probe definitions.
> ...
> -----
>
> Reported-by: Jiri Olsa <[email protected]>

works nicely, thanks a lot!

Tested-by: Jiri Olsa <[email protected]>

jirka

Subject: Re: [PATCH perf/core 2/2] perf-probe: Check the orphaned -x option

Ping?

(2015/04/01 19:25), Masami Hiramatsu wrote:
> To avoid probing in unintended binary, the orphaned -x option
> must be checked and warned.
>
> Without this patch, following command sets up the probe in
> the kernel.
> -----
> # perf probe -a strcpy -x ./perf
> Added new event:
> probe:strcpy (on strcpy)
>
> You can now use it in all perf tools, such as:
>
> perf record -e probe:strcpy -aR sleep 1
> -----
>
> But in this case, it seems that the user may want to probe
> in the perf binary. With this patch, perf-probe correctly
> handles the orphaned -x.
> -----
> # perf probe -a strcpy -x ./perf
> Error: -x/-m must follow the probe definitions.
> ...
> -----
>
> Reported-by: Jiri Olsa <[email protected]>
> Signed-off-by: Masami Hiramatsu <[email protected]>
> ---
> tools/perf/builtin-probe.c | 10 ++++++++++
> 1 file changed, 10 insertions(+)
>
> diff --git a/tools/perf/builtin-probe.c b/tools/perf/builtin-probe.c
> index 2df23e1..f7b1af6 100644
> --- a/tools/perf/builtin-probe.c
> +++ b/tools/perf/builtin-probe.c
> @@ -56,6 +56,7 @@ static struct {
> bool mod_events;
> bool uprobes;
> bool quiet;
> + bool target_used;
> int nevents;
> struct perf_probe_event events[MAX_PROBES];
> struct strlist *dellist;
> @@ -82,6 +83,7 @@ static int parse_probe_event(const char *str)
> pev->target = strdup(params.target);
> if (!pev->target)
> return -ENOMEM;
> + params.target_used = true;
> }
>
> /* Parse a perf-probe command into event */
> @@ -107,6 +109,7 @@ static int set_target(const char *ptr)
> params.target = strdup(ptr);
> if (!params.target)
> return -ENOMEM;
> + params.target_used = false;
>
> found = 1;
> buf = ptr + (strlen(ptr) - 3);
> @@ -207,6 +210,7 @@ static int opt_set_target(const struct option *opt, const char *str,
> }
> free(params.target);
> params.target = tmp;
> + params.target_used = false;
> ret = 0;
> }
>
> @@ -491,6 +495,12 @@ __cmd_probe(int argc, const char **argv, const char *prefix __maybe_unused)
> }
>
> if (params.nevents) {
> + /* Ensure the last given target is used */
> + if (params.target && !params.target_used) {
> + pr_warning(" Error: -x/-m must follow the probe definitions.\n");
> + usage_with_options(probe_usage, options);
> + }
> +
> ret = add_perf_probe_events(params.events, params.nevents,
> params.max_probe_points,
> params.force_add);
>
>


--
Masami HIRAMATSU
Linux Technology Research Center, System Productivity Research Dept.
Center for Technology Innovation - Systems Engineering
Hitachi, Ltd., Research & Development Group
E-mail: [email protected]

2015-04-10 13:22:29

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: Re: [PATCH perf/core 2/2] perf-probe: Check the orphaned -x option

Em Fri, Apr 10, 2015 at 03:51:57PM +0900, Masami Hiramatsu escreveu:
> Ping?

Sorry for the delay, thought I had processed those already, done now,
will be in my next pull req.

Thanks for working on it, I tested both and all seems fine,

- Arnaldo

> (2015/04/01 19:25), Masami Hiramatsu wrote:
> > To avoid probing in unintended binary, the orphaned -x option
> > must be checked and warned.
> >
> > Without this patch, following command sets up the probe in
> > the kernel.
> > -----
> > # perf probe -a strcpy -x ./perf
> > Added new event:
> > probe:strcpy (on strcpy)
> >
> > You can now use it in all perf tools, such as:
> >
> > perf record -e probe:strcpy -aR sleep 1
> > -----
> >
> > But in this case, it seems that the user may want to probe
> > in the perf binary. With this patch, perf-probe correctly
> > handles the orphaned -x.
> > -----
> > # perf probe -a strcpy -x ./perf
> > Error: -x/-m must follow the probe definitions.
> > ...
> > -----
> >
> > Reported-by: Jiri Olsa <[email protected]>
> > Signed-off-by: Masami Hiramatsu <[email protected]>
> > ---
> > tools/perf/builtin-probe.c | 10 ++++++++++
> > 1 file changed, 10 insertions(+)
> >
> > diff --git a/tools/perf/builtin-probe.c b/tools/perf/builtin-probe.c
> > index 2df23e1..f7b1af6 100644
> > --- a/tools/perf/builtin-probe.c
> > +++ b/tools/perf/builtin-probe.c
> > @@ -56,6 +56,7 @@ static struct {
> > bool mod_events;
> > bool uprobes;
> > bool quiet;
> > + bool target_used;
> > int nevents;
> > struct perf_probe_event events[MAX_PROBES];
> > struct strlist *dellist;
> > @@ -82,6 +83,7 @@ static int parse_probe_event(const char *str)
> > pev->target = strdup(params.target);
> > if (!pev->target)
> > return -ENOMEM;
> > + params.target_used = true;
> > }
> >
> > /* Parse a perf-probe command into event */
> > @@ -107,6 +109,7 @@ static int set_target(const char *ptr)
> > params.target = strdup(ptr);
> > if (!params.target)
> > return -ENOMEM;
> > + params.target_used = false;
> >
> > found = 1;
> > buf = ptr + (strlen(ptr) - 3);
> > @@ -207,6 +210,7 @@ static int opt_set_target(const struct option *opt, const char *str,
> > }
> > free(params.target);
> > params.target = tmp;
> > + params.target_used = false;
> > ret = 0;
> > }
> >
> > @@ -491,6 +495,12 @@ __cmd_probe(int argc, const char **argv, const char *prefix __maybe_unused)
> > }
> >
> > if (params.nevents) {
> > + /* Ensure the last given target is used */
> > + if (params.target && !params.target_used) {
> > + pr_warning(" Error: -x/-m must follow the probe definitions.\n");
> > + usage_with_options(probe_usage, options);
> > + }
> > +
> > ret = add_perf_probe_events(params.events, params.nevents,
> > params.max_probe_points,
> > params.force_add);
> >
> >
>
>
> --
> Masami HIRAMATSU
> Linux Technology Research Center, System Productivity Research Dept.
> Center for Technology Innovation - Systems Engineering
> Hitachi, Ltd., Research & Development Group
> E-mail: [email protected]
>

Subject: [tip:perf/core] perf probe: Check the orphaned -x option

Commit-ID: 8cb0aa4c2db395b143cd5165586dc17677960002
Gitweb: http://git.kernel.org/tip/8cb0aa4c2db395b143cd5165586dc17677960002
Author: Masami Hiramatsu <[email protected]>
AuthorDate: Wed, 1 Apr 2015 19:25:42 +0900
Committer: Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Fri, 10 Apr 2015 10:21:30 -0300

perf probe: Check the orphaned -x option

To avoid probing in unintended binary, the orphaned -x option must be
checked and warned.

Without this patch, following command sets up the probe in the kernel.

-----
# perf probe -a strcpy -x ./perf
Added new event:
probe:strcpy (on strcpy)

You can now use it in all perf tools, such as:

perf record -e probe:strcpy -aR sleep 1
-----

But in this case, it seems that the user may want to probe in the perf
binary. With this patch, perf-probe correctly handles the orphaned -x.

-----
# perf probe -a strcpy -x ./perf
Error: -x/-m must follow the probe definitions.
...
-----

Reported-by: Jiri Olsa <[email protected]>
Acked-by: Jiri Olsa <[email protected]>
Cc: David Ahern <[email protected]>
Cc: Namhyung Kim <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Masami Hiramatsu <[email protected]>
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
tools/perf/builtin-probe.c | 10 ++++++++++
1 file changed, 10 insertions(+)

diff --git a/tools/perf/builtin-probe.c b/tools/perf/builtin-probe.c
index 2df23e1..f7b1af6 100644
--- a/tools/perf/builtin-probe.c
+++ b/tools/perf/builtin-probe.c
@@ -56,6 +56,7 @@ static struct {
bool mod_events;
bool uprobes;
bool quiet;
+ bool target_used;
int nevents;
struct perf_probe_event events[MAX_PROBES];
struct strlist *dellist;
@@ -82,6 +83,7 @@ static int parse_probe_event(const char *str)
pev->target = strdup(params.target);
if (!pev->target)
return -ENOMEM;
+ params.target_used = true;
}

/* Parse a perf-probe command into event */
@@ -107,6 +109,7 @@ static int set_target(const char *ptr)
params.target = strdup(ptr);
if (!params.target)
return -ENOMEM;
+ params.target_used = false;

found = 1;
buf = ptr + (strlen(ptr) - 3);
@@ -207,6 +210,7 @@ static int opt_set_target(const struct option *opt, const char *str,
}
free(params.target);
params.target = tmp;
+ params.target_used = false;
ret = 0;
}

@@ -491,6 +495,12 @@ __cmd_probe(int argc, const char **argv, const char *prefix __maybe_unused)
}

if (params.nevents) {
+ /* Ensure the last given target is used */
+ if (params.target && !params.target_used) {
+ pr_warning(" Error: -x/-m must follow the probe definitions.\n");
+ usage_with_options(probe_usage, options);
+ }
+
ret = add_perf_probe_events(params.events, params.nevents,
params.max_probe_points,
params.force_add);