The check is a common part of the ftrace commands, let's move it out.
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
tools/perf/builtin-ftrace.c | 44 +++++++++++++++++--------------------
1 file changed, 20 insertions(+), 24 deletions(-)
diff --git a/tools/perf/builtin-ftrace.c b/tools/perf/builtin-ftrace.c
index 33c52ebda2fd..978608ecd89c 100644
--- a/tools/perf/builtin-ftrace.c
+++ b/tools/perf/builtin-ftrace.c
@@ -59,6 +59,22 @@ static void ftrace__workload_exec_failed_signal(int signo __maybe_unused,
done = true;
}
+static int check_ftrace_capable(void)
+{
+ if (!(perf_cap__capable(CAP_PERFMON) ||
+ perf_cap__capable(CAP_SYS_ADMIN))) {
+ pr_err("ftrace only works for %s!\n",
+#ifdef HAVE_LIBCAP_SUPPORT
+ "users with the CAP_PERFMON or CAP_SYS_ADMIN capability"
+#else
+ "root"
+#endif
+ );
+ return -1;
+ }
+ return 0;
+}
+
static int __write_tracing_file(const char *name, const char *val, bool append)
{
char *file;
@@ -586,18 +602,6 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace)
.events = POLLIN,
};
- if (!(perf_cap__capable(CAP_PERFMON) ||
- perf_cap__capable(CAP_SYS_ADMIN))) {
- pr_err("ftrace only works for %s!\n",
-#ifdef HAVE_LIBCAP_SUPPORT
- "users with the CAP_PERFMON or CAP_SYS_ADMIN capability"
-#else
- "root"
-#endif
- );
- return -1;
- }
-
select_tracer(ftrace);
if (reset_tracing_files(ftrace) < 0) {
@@ -902,18 +906,6 @@ static int __cmd_latency(struct perf_ftrace *ftrace)
};
int buckets[NUM_BUCKET] = { };
- if (!(perf_cap__capable(CAP_PERFMON) ||
- perf_cap__capable(CAP_SYS_ADMIN))) {
- pr_err("ftrace only works for %s!\n",
-#ifdef HAVE_LIBCAP_SUPPORT
- "users with the CAP_PERFMON or CAP_SYS_ADMIN capability"
-#else
- "root"
-#endif
- );
- return -1;
- }
-
trace_fd = prepare_func_latency(ftrace);
if (trace_fd < 0)
goto out;
@@ -1220,6 +1212,10 @@ int cmd_ftrace(int argc, const char **argv)
signal(SIGCHLD, sig_handler);
signal(SIGPIPE, sig_handler);
+ ret = check_ftrace_capable();
+ if (ret < 0)
+ return -1;
+
ret = perf_config(perf_ftrace_config, &ftrace);
if (ret < 0)
return -1;
--
2.46.0.rc1.232.g9752f9e123-goog
On Sun, Jul 28, 2024 at 5:41 PM Namhyung Kim <namhyung@kernel.org> wrote:
>
> The check is a common part of the ftrace commands, let's move it out.
>
> Signed-off-by: Namhyung Kim <namhyung@kernel.org>
The whole ifdefs and perf_cap__capable seemed wrong, not least as we'd
like the error message to be accurate not just when libcap is present,
so I sent out;
https://lore.kernel.org/lkml/20240729181931.2870851-1-irogers@google.com/
which I think can supersede this.
Thanks,
Ian
> ---
> tools/perf/builtin-ftrace.c | 44 +++++++++++++++++--------------------
> 1 file changed, 20 insertions(+), 24 deletions(-)
>
> diff --git a/tools/perf/builtin-ftrace.c b/tools/perf/builtin-ftrace.c
> index 33c52ebda2fd..978608ecd89c 100644
> --- a/tools/perf/builtin-ftrace.c
> +++ b/tools/perf/builtin-ftrace.c
> @@ -59,6 +59,22 @@ static void ftrace__workload_exec_failed_signal(int signo __maybe_unused,
> done = true;
> }
>
> +static int check_ftrace_capable(void)
> +{
> + if (!(perf_cap__capable(CAP_PERFMON) ||
> + perf_cap__capable(CAP_SYS_ADMIN))) {
> + pr_err("ftrace only works for %s!\n",
> +#ifdef HAVE_LIBCAP_SUPPORT
> + "users with the CAP_PERFMON or CAP_SYS_ADMIN capability"
> +#else
> + "root"
> +#endif
> + );
> + return -1;
> + }
> + return 0;
> +}
> +
> static int __write_tracing_file(const char *name, const char *val, bool append)
> {
> char *file;
> @@ -586,18 +602,6 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace)
> .events = POLLIN,
> };
>
> - if (!(perf_cap__capable(CAP_PERFMON) ||
> - perf_cap__capable(CAP_SYS_ADMIN))) {
> - pr_err("ftrace only works for %s!\n",
> -#ifdef HAVE_LIBCAP_SUPPORT
> - "users with the CAP_PERFMON or CAP_SYS_ADMIN capability"
> -#else
> - "root"
> -#endif
> - );
> - return -1;
> - }
> -
> select_tracer(ftrace);
>
> if (reset_tracing_files(ftrace) < 0) {
> @@ -902,18 +906,6 @@ static int __cmd_latency(struct perf_ftrace *ftrace)
> };
> int buckets[NUM_BUCKET] = { };
>
> - if (!(perf_cap__capable(CAP_PERFMON) ||
> - perf_cap__capable(CAP_SYS_ADMIN))) {
> - pr_err("ftrace only works for %s!\n",
> -#ifdef HAVE_LIBCAP_SUPPORT
> - "users with the CAP_PERFMON or CAP_SYS_ADMIN capability"
> -#else
> - "root"
> -#endif
> - );
> - return -1;
> - }
> -
> trace_fd = prepare_func_latency(ftrace);
> if (trace_fd < 0)
> goto out;
> @@ -1220,6 +1212,10 @@ int cmd_ftrace(int argc, const char **argv)
> signal(SIGCHLD, sig_handler);
> signal(SIGPIPE, sig_handler);
>
> + ret = check_ftrace_capable();
> + if (ret < 0)
> + return -1;
> +
> ret = perf_config(perf_ftrace_config, &ftrace);
> if (ret < 0)
> return -1;
> --
> 2.46.0.rc1.232.g9752f9e123-goog
>
Hi Ian,
On Mon, Jul 29, 2024 at 11:23 AM Ian Rogers <irogers@google.com> wrote:
>
> On Sun, Jul 28, 2024 at 5:41 PM Namhyung Kim <namhyung@kernel.org> wrote:
> >
> > The check is a common part of the ftrace commands, let's move it out.
> >
> > Signed-off-by: Namhyung Kim <namhyung@kernel.org>
>
> The whole ifdefs and perf_cap__capable seemed wrong, not least as we'd
> like the error message to be accurate not just when libcap is present,
> so I sent out;
> https://lore.kernel.org/lkml/20240729181931.2870851-1-irogers@google.com/
> which I think can supersede this.
Thanks for the change! I'll reply to the patch.
Namhyung
>
> > ---
> > tools/perf/builtin-ftrace.c | 44 +++++++++++++++++--------------------
> > 1 file changed, 20 insertions(+), 24 deletions(-)
> >
> > diff --git a/tools/perf/builtin-ftrace.c b/tools/perf/builtin-ftrace.c
> > index 33c52ebda2fd..978608ecd89c 100644
> > --- a/tools/perf/builtin-ftrace.c
> > +++ b/tools/perf/builtin-ftrace.c
> > @@ -59,6 +59,22 @@ static void ftrace__workload_exec_failed_signal(int signo __maybe_unused,
> > done = true;
> > }
> >
> > +static int check_ftrace_capable(void)
> > +{
> > + if (!(perf_cap__capable(CAP_PERFMON) ||
> > + perf_cap__capable(CAP_SYS_ADMIN))) {
> > + pr_err("ftrace only works for %s!\n",
> > +#ifdef HAVE_LIBCAP_SUPPORT
> > + "users with the CAP_PERFMON or CAP_SYS_ADMIN capability"
> > +#else
> > + "root"
> > +#endif
> > + );
> > + return -1;
> > + }
> > + return 0;
> > +}
> > +
> > static int __write_tracing_file(const char *name, const char *val, bool append)
> > {
> > char *file;
> > @@ -586,18 +602,6 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace)
> > .events = POLLIN,
> > };
> >
> > - if (!(perf_cap__capable(CAP_PERFMON) ||
> > - perf_cap__capable(CAP_SYS_ADMIN))) {
> > - pr_err("ftrace only works for %s!\n",
> > -#ifdef HAVE_LIBCAP_SUPPORT
> > - "users with the CAP_PERFMON or CAP_SYS_ADMIN capability"
> > -#else
> > - "root"
> > -#endif
> > - );
> > - return -1;
> > - }
> > -
> > select_tracer(ftrace);
> >
> > if (reset_tracing_files(ftrace) < 0) {
> > @@ -902,18 +906,6 @@ static int __cmd_latency(struct perf_ftrace *ftrace)
> > };
> > int buckets[NUM_BUCKET] = { };
> >
> > - if (!(perf_cap__capable(CAP_PERFMON) ||
> > - perf_cap__capable(CAP_SYS_ADMIN))) {
> > - pr_err("ftrace only works for %s!\n",
> > -#ifdef HAVE_LIBCAP_SUPPORT
> > - "users with the CAP_PERFMON or CAP_SYS_ADMIN capability"
> > -#else
> > - "root"
> > -#endif
> > - );
> > - return -1;
> > - }
> > -
> > trace_fd = prepare_func_latency(ftrace);
> > if (trace_fd < 0)
> > goto out;
> > @@ -1220,6 +1212,10 @@ int cmd_ftrace(int argc, const char **argv)
> > signal(SIGCHLD, sig_handler);
> > signal(SIGPIPE, sig_handler);
> >
> > + ret = check_ftrace_capable();
> > + if (ret < 0)
> > + return -1;
> > +
> > ret = perf_config(perf_ftrace_config, &ftrace);
> > if (ret < 0)
> > return -1;
> > --
> > 2.46.0.rc1.232.g9752f9e123-goog
> >
© 2016 - 2026 Red Hat, Inc.