[1/1] perf beauty: Don't use 'find ... -printf' as it isn't available in busybox

Message ID ZWkEeqdmCHMLhLr2@kernel.org
State New
Headers
Series [1/1] perf beauty: Don't use 'find ... -printf' as it isn't available in busybox |

Commit Message

Arnaldo Carvalho de Melo Nov. 30, 2023, 9:54 p.m. UTC
  Namhyung reported:

  I'm seeing a build error on my Alpine linux image which uses busybox +
  musl libc:

    In file included from trace/beauty/arch_errno_names.c:1,
                     from builtin-trace.c:899:
    /build/trace/beauty/generated/arch_errno_name_array.c: In function 'arch_syscalls__strerrno':
    /build/trace/beauty/generated/arch_errno_name_array.c:142:49: error: unused parameter 'arch' [-Werror=unused-parameter]
      142 | const char *arch_syscalls__strerrno(const char *arch, int err)

  It looks like busybox find command doesn't have -printf option

    find: unrecognized: -printf
    , Yesterday 9:16 PM
    ,
    BusyBox v1.36.1 (2023-07-27 17:12:24 UTC) multi-call binary.

    Usage: find [-HL] [PATH]... [OPTIONS] [ACTIONS]

    Search for files and perform actions on them.
    First failed action stops processing of current file.
    Defaults: PATH is current directory, action is '-print'

So just remove it and pipe find's entry to a basename loop to produce
the same result.

Fixes: 0337cf74ccf2a434 ("perf util: Introduce architecture specific errno/name mapping")
Reported-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Michael Petlan <mpetlan@redhat.com>
Cc: Thomas Richter <tmricht@linux.vnet.ibm.com>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/trace/beauty/arch_errno_names.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
  

Comments

Namhyung Kim Nov. 30, 2023, 10:12 p.m. UTC | #1
Hi Arnaldo,

On Thu, Nov 30, 2023 at 1:54 PM Arnaldo Carvalho de Melo
<acme@kernel.org> wrote:
>
> Namhyung reported:
>
>   I'm seeing a build error on my Alpine linux image which uses busybox +
>   musl libc:
>
>     In file included from trace/beauty/arch_errno_names.c:1,
>                      from builtin-trace.c:899:
>     /build/trace/beauty/generated/arch_errno_name_array.c: In function 'arch_syscalls__strerrno':
>     /build/trace/beauty/generated/arch_errno_name_array.c:142:49: error: unused parameter 'arch' [-Werror=unused-parameter]
>       142 | const char *arch_syscalls__strerrno(const char *arch, int err)
>
>   It looks like busybox find command doesn't have -printf option
>
>     find: unrecognized: -printf
>     , Yesterday 9:16 PM
>     ,
>     BusyBox v1.36.1 (2023-07-27 17:12:24 UTC) multi-call binary.
>
>     Usage: find [-HL] [PATH]... [OPTIONS] [ACTIONS]
>
>     Search for files and perform actions on them.
>     First failed action stops processing of current file.
>     Defaults: PATH is current directory, action is '-print'
>
> So just remove it and pipe find's entry to a basename loop to produce
> the same result.
>
> Fixes: 0337cf74ccf2a434 ("perf util: Introduce architecture specific errno/name mapping")
> Reported-by: Namhyung Kim <namhyung@kernel.org>
> Cc: Adrian Hunter <adrian.hunter@intel.com>
> Cc: Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
> Cc: Ian Rogers <irogers@google.com>
> Cc: Jiri Olsa <jolsa@kernel.org>
> Cc: Michael Petlan <mpetlan@redhat.com>
> Cc: Thomas Richter <tmricht@linux.vnet.ibm.com>
> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

Thanks for the fix!

Tested-by: Namhyung Kim <namhyung@kernel.org>

Thanks,
Namhyung

> ---
>  tools/perf/trace/beauty/arch_errno_names.sh | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/tools/perf/trace/beauty/arch_errno_names.sh b/tools/perf/trace/beauty/arch_errno_names.sh
> index cc09dcaa891e04bb..3ec8781344db13ba 100755
> --- a/tools/perf/trace/beauty/arch_errno_names.sh
> +++ b/tools/perf/trace/beauty/arch_errno_names.sh
> @@ -76,7 +76,7 @@ EoHEADER
>
>  # Create list of architectures that have a specific errno.h.
>  archlist=""
> -for arch in $(find $toolsdir/arch -maxdepth 1 -mindepth 1 -type d -printf "%f\n" | sort -r); do
> +for arch in $(find $toolsdir/arch -maxdepth 1 -mindepth 1 -type d | while read arch ; do basename $arch ; done | sort -r); do
>         test -f $toolsdir/arch/$arch/include/uapi/asm/errno.h && archlist="$archlist $arch"
>  done
>
> --
> 2.41.0
>
  
David Laight Dec. 1, 2023, 12:05 p.m. UTC | #2
...
>  # Create list of architectures that have a specific errno.h.
>  archlist=""
> -for arch in $(find $toolsdir/arch -maxdepth 1 -mindepth 1 -type d -printf "%f\n" | sort -r); do
> +for arch in $(find $toolsdir/arch -maxdepth 1 -mindepth 1 -type d | while read arch ; do basename
> $arch ; done | sort -r); do
>  	test -f $toolsdir/arch/$arch/include/uapi/asm/errno.h && archlist="$archlist $arch"
>  done

Jeepers ...
Does this work?
	for f in $toolsdir/arch/*/include/uapi/asm/errno.h; do
		[ ! -f $f ] && break
		d=${f%/include/uapi/asm/errno.h}
		archlist="${d##*/} $archlist"
	done
No fork()s or exec()s.
I think it only differs in having a trailing space instead of a leading one.

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
  
Arnaldo Carvalho de Melo Dec. 1, 2023, 5:01 p.m. UTC | #3
Em Fri, Dec 01, 2023 at 12:05:31PM +0000, David Laight escreveu:
> ...
> >  # Create list of architectures that have a specific errno.h.
> >  archlist=""
> > -for arch in $(find $toolsdir/arch -maxdepth 1 -mindepth 1 -type d -printf "%f\n" | sort -r); do
> > +for arch in $(find $toolsdir/arch -maxdepth 1 -mindepth 1 -type d | while read arch ; do basename
> > $arch ; done | sort -r); do
> >  	test -f $toolsdir/arch/$arch/include/uapi/asm/errno.h && archlist="$archlist $arch"
> >  done
> 
> Jeepers ...
> Does this work?
> 	for f in $toolsdir/arch/*/include/uapi/asm/errno.h; do
> 		[ ! -f $f ] && break
> 		d=${f%/include/uapi/asm/errno.h}
> 		archlist="${d##*/} $archlist"
> 	done
> No fork()s or exec()s.
> I think it only differs in having a trailing space instead of a leading one.

⬢[acme@toolbox perf-tools-next]$ for f in tools/arch/*/include/uapi/asm/errno.h; do d=${f%/include/uapi/asm/errno.h} ; arch="${d##*/}" ; echo "'$arch'" ; done
'alpha'
'mips'
'parisc'
'powerpc'
'sparc'
'x86'
⬢[acme@toolbox perf-tools-next]$ for arch in $(find tools/arch -maxdepth 1 -mindepth 1 -type d | while read arch ; do basename $arch ; done | sort -r) ; do test -f tools/arch/$arch/include/uapi/asm/errno.h && echo "'$arch'" ; done
'x86'
'sparc'
'powerpc'
'parisc'
'mips'
'alpha'
⬢[acme@toolbox perf-tools-next]$

There was a reason for having x86 first, lemme dig it... Just to have
as the first strcmp in:

const char *arch_syscalls__strerrno(const char *arch, int err)
{
        if (!strcmp(arch, "x86"))
                return errno_to_name__x86(err);
        if (!strcmp(arch, "sparc"))
                return errno_to_name__sparc(err);
        if (!strcmp(arch, "powerpc"))
                return errno_to_name__powerpc(err);
        if (!strcmp(arch, "parisc"))
                return errno_to_name__parisc(err);
        if (!strcmp(arch, "mips"))
                return errno_to_name__mips(err);
        if (!strcmp(arch, "alpha"))
                return errno_to_name__alpha(err);
        return errno_to_name__generic(err);
}

But that is a weak reason, we better make users resolve the right
errno_to_name__$arch() pointer just once and use it without that strcmp.

Will do it in a follow up patch.

Thanks, the resulting diff is below, but I'll first do changes that will
remove the need for arch_syscalls__strerrno.

diff --git a/tools/perf/trace/beauty/arch_errno_names.sh b/tools/perf/trace/beauty/arch_errno_names.sh
index 3ec8781344db13ba..b6e0767b4b34e46a 100755
--- a/tools/perf/trace/beauty/arch_errno_names.sh
+++ b/tools/perf/trace/beauty/arch_errno_names.sh
@@ -76,7 +76,9 @@ EoHEADER
 
 # Create list of architectures that have a specific errno.h.
 archlist=""
-for arch in $(find $toolsdir/arch -maxdepth 1 -mindepth 1 -type d | while read arch ; do basename $arch ; done | sort -r); do
+for f in $toolsdir/arch/*/include/uapi/asm/errno.h; do
+	d=${f%/include/uapi/asm/errno.h}
+	arch="${d##*/}"
 	test -f $toolsdir/arch/$arch/include/uapi/asm/errno.h && archlist="$archlist $arch"
 done
  
David Laight Dec. 1, 2023, 5:16 p.m. UTC | #4
From: Arnaldo Carvalho de Melo
> Sent: 01 December 2023 17:02
> 
> Em Fri, Dec 01, 2023 at 12:05:31PM +0000, David Laight escreveu:
> > ...
> > >  # Create list of architectures that have a specific errno.h.
> > >  archlist=""
> > > -for arch in $(find $toolsdir/arch -maxdepth 1 -mindepth 1 -type d -printf "%f\n" | sort -r); do
> > > +for arch in $(find $toolsdir/arch -maxdepth 1 -mindepth 1 -type d | while read arch ; do basename
> > > $arch ; done | sort -r); do
> > >  	test -f $toolsdir/arch/$arch/include/uapi/asm/errno.h && archlist="$archlist $arch"
> > >  done
> >
> > Jeepers ...
> > Does this work?
> > 	for f in $toolsdir/arch/*/include/uapi/asm/errno.h; do
> > 		[ ! -f $f ] && break
> > 		d=${f%/include/uapi/asm/errno.h}
> > 		archlist="${d##*/} $archlist"
> > 	done
> > No fork()s or exec()s.
> > I think it only differs in having a trailing space instead of a leading one.
> 
> ⬢[acme@toolbox perf-tools-next]$ for f in tools/arch/*/include/uapi/asm/errno.h; do
> d=${f%/include/uapi/asm/errno.h} ; arch="${d##*/}" ; echo "'$arch'" ; done
> 'alpha'
> 'mips'
> 'parisc'
> 'powerpc'
> 'sparc'
> 'x86'
> ⬢[acme@toolbox perf-tools-next]$ for arch in $(find tools/arch -maxdepth 1 -mindepth 1 -type d |
> while read arch ; do basename $arch ; done | sort -r) ; do test -f
> tools/arch/$arch/include/uapi/asm/errno.h && echo "'$arch'" ; done
> 'x86'
> 'sparc'
> 'powerpc'
> 'parisc'
> 'mips'
> 'alpha'
> ⬢[acme@toolbox perf-tools-next]$
> 
> There was a reason for having x86 first, lemme dig it... Just to have
> as the first strcmp in:

I reversed the order by adding the names at the front of $archlist - so the order
would still be reverse sorted (the shell sorts filename globs).

The '[ -f $f ] || break' line is only there in case there are no matching files.
If that can happen putting [ "$archlist" = "* " ] && archlist=
after the loop would be slightly better.
But with only 6 item the extra stat() will be noise.

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
  

Patch

diff --git a/tools/perf/trace/beauty/arch_errno_names.sh b/tools/perf/trace/beauty/arch_errno_names.sh
index cc09dcaa891e04bb..3ec8781344db13ba 100755
--- a/tools/perf/trace/beauty/arch_errno_names.sh
+++ b/tools/perf/trace/beauty/arch_errno_names.sh
@@ -76,7 +76,7 @@  EoHEADER
 
 # Create list of architectures that have a specific errno.h.
 archlist=""
-for arch in $(find $toolsdir/arch -maxdepth 1 -mindepth 1 -type d -printf "%f\n" | sort -r); do
+for arch in $(find $toolsdir/arch -maxdepth 1 -mindepth 1 -type d | while read arch ; do basename $arch ; done | sort -r); do
 	test -f $toolsdir/arch/$arch/include/uapi/asm/errno.h && archlist="$archlist $arch"
 done