perf tools: Fix arm64 build error with gcc-11
PATCH v4 1/1 perf tools: Fix arm64 build error with gcc-11
gcc version: 11.0.0 20210208 (experimental) (GCC)
Following build error on arm64:
.......
In function ‘printf’,
inlined from ‘regs_dump__printf’ at util/session.c:1141:3, inlined from ‘regs__printf’ at util/session.c:1169:2:/usr/include/aarch64-linux-gnu/bits/stdio2.h:107:10: \
error: ‘%-5s’ directive argument is null [-Werror=format-overflow=]
107 | return __printf_chk (__USE_FORTIFY_LEVEL - 1, __fmt, \
__va_arg_pack ());......
In function ‘fprintf’,
inlined from ‘perf_sample__fprintf_regs.isra’ at \
builtin-script.c:622:14:/usr/include/aarch64-linux-gnu/bits/stdio2.h:100:10: \
error: ‘%5s’ directive argument is null [-Werror=format-overflow=]
100 | return __fprintf_chk (__stream, __USE_FORTIFY_LEVEL - 1, __fmt,
101 | __va_arg_pack ());
cc1: all warnings being treated as errors
.......
This patch fixes Wformat-overflow warnings. Add helper function to convert NULL to "unknown".
---
v2: Add ternary operator to avoid similar errors in other arch.
v3: Declared reg_name in inner block.
v4: Add helper function: perf_reg_name_str, update changelog.
--- tools/perf/builtin-script.c | 2 +- tools/perf/util/perf_regs.h | 11 ++++++++++- .../perf/util/scripting-engines/trace-event-python.c | 2 +- tools/perf/util/session.c | 2 +- 4 files changed, 13 insertions(+), 4 deletions(-)
tools/perf/builtin-script.c +1 -1
@@ -643,7 +643,7 @@ static int perf_sample__fprintf_regs(struct regs_dump *regs, uint64_t mask,for_each_set_bit (r, (unsigned long *) &mask,sizeof (mask) *8 ) {u64 val = regs->regs[i++]; - printed +=fprintf (fp,"%5s:0x%" PRIx64" " ,perf_reg_name (r), val); + printed +=fprintf (fp,"%5s:0x%" PRIx64" " ,perf_reg_name_str (r), val); }return printed;
tools/perf/util/perf_regs.h +10 -1
@@ -33,13 +33,22 @@ extern const struct sample_reg sample_reg_masks[];int perf_reg_value (u64 *valp,struct regs_dump *regs,int id); +static inline const char *perf_reg_name_str (int id) +{ +const char *str =perf_reg_name (id); + +if (!str) +return "unknown" ; +return str; +} +#else #define PERF_REGS_MASK 0#define PERF_REGS_MAX 0#define DWARF_MINIMAL_REGS PERF_REGS_MASK -static inline const char *perf_reg_name (int id__maybe_unused ) +static inline const char *perf_reg_name_str (int id__maybe_unused ) {return "unknown" ; }
tools/perf/util/scripting-engines/trace-event-python.c +1 -1
@@ -702,7 +702,7 @@ static int regs_map(struct regs_dump *regs, uint64_t mask, char *bf, int size) printed +=scnprintf (bf + printed, size - printed,"%5s:0x%" PRIx64" " , -perf_reg_name (r), val); +perf_reg_name_str (r), val); }return printed;
tools/perf/util/session.c +1 -1
@@ -1140,7 +1140,7 @@ static void regs_dump__printf(u64 mask, u64 *regs)u64 val = regs[i++];printf (".... %-5s 0x%016" PRIx64"\n" , -perf_reg_name (rid), val); +perf_reg_name_str (rid), val); } }
Signature · 1 line
2.25.1
Arnaldo Carvalho de Melo
maintainerEm Wed, Feb 17, 2021 at 07:58:30PM +0800, Jianlin Lv escreveu:
Quoted 23 lines
gcc version: 11.0.0 20210208 (experimental) (GCC)
Following build error on arm64:
.......
In function ‘printf’,
inlined from ‘regs_dump__printf’ atutil/session.c:1141:3, inlined from ‘regs__printf’ atutil/session.c:1169:2:/usr/include/aarch64-linux-gnu/bits/stdio2.h:107:10: \
error: ‘%-5s’ directive argument is null [-Werror=format-overflow=]107 | return __printf_chk (__USE_FORTIFY_LEVEL - 1, __fmt, \ __va_arg_pack ());......
In function ‘fprintf’,inlined from ‘perf_sample__fprintf_regs.isra’ at \ builtin-script.c:622:14:
/usr/include/aarch64-linux-gnu/bits/stdio2.h:100:10: \
error: ‘%5s’ directive argument is null [-Werror=format-overflow=]
100 | return__fprintf_chk(__stream,__USE_FORTIFY_LEVEL- 1,__fmt,
101 |__va_arg_pack());
Quoted 5 lines
cc1: all warnings being treated as errors
.......This patch fixes Wformat-overflow warnings. Add helper function to convert NULL to "unknown".
I think this is the right approach, but since both return a string, it is strange that only one of them have its _str() at the end, what we usually do in such cases is to have:
const char *__perf_reg_name(int id)
{
return NULL if id unknown;
}And:
static inline const char *perf_reg_name(int id)
{
const char *name = __perf_reg_name(id); return name ?: "unknown";
}
Ok?
- Arnaldo
Quoted 82 lines
Signed-off-by Jianlin Lv <[email protected]>---
v2: Add ternary operator to avoid similar errors in other arch.
v3: Declaredreg_namein inner block.
v4: Add helper function:perf_reg_name_str, update changelog.--- tools/perf/builtin-script.c | 2 +- tools/perf/util/perf_regs.h | 11 ++++++++++- .../perf/util/scripting-engines/trace-event-python.c | 2 +- tools/perf/util/session.c | 2 +- 4 files changed, 13 insertions(+), 4 deletions(-)tools/perf/builtin-script.c +1
-1@@ -643,7 +643,7 @@ static int perf_sample__fprintf_regs(struct regs_dump *regs, uint64_t mask,for_each_set_bit (r, (unsigned long *) &mask,sizeof (mask) *8 ) {u64 val = regs->regs[i++]; - printed +=fprintf (fp,"%5s:0x%" PRIx64" " ,perf_reg_name (r), val); + printed +=fprintf (fp,"%5s:0x%" PRIx64" " ,perf_reg_name_str (r), val); }return printed;tools/perf/util/perf_regs.h +10
-1@@ -33,13 +33,22 @@ extern const struct sample_reg sample_reg_masks[];int perf_reg_value (u64 *valp,struct regs_dump *regs,int id); +static inline const char *perf_reg_name_str (int id) +{ +const char *str =perf_reg_name (id); + +if (!str) +return "unknown" ; +return str; +} +#else #define PERF_REGS_MASK 0#define PERF_REGS_MAX 0#define DWARF_MINIMAL_REGS PERF_REGS_MASK -static inline const char *perf_reg_name (int id__maybe_unused ) +static inline const char *perf_reg_name_str (int id__maybe_unused ) {return "unknown" ; }tools/perf/util/scripting-engines/trace-event-python.c +1
-1@@ -702,7 +702,7 @@ static int regs_map(struct regs_dump *regs, uint64_t mask, char *bf, int size) printed +=scnprintf (bf + printed, size - printed,"%5s:0x%" PRIx64" " , -perf_reg_name (r), val); +perf_reg_name_str (r), val); }return printed;tools/perf/util/session.c +1
-1@@ -1140,7 +1140,7 @@ static void regs_dump__printf(u64 mask, u64 *regs)u64 val = regs[i++];printf (".... %-5s 0x%016" PRIx64"\n" , -perf_reg_name (rid), val); +perf_reg_name_str (rid), val); } }Signature · 1 line
2.25.1
Signature · 1 line
- Arnaldo