[PATCH v3 01/12] perf script: Make printing flags reliable

Adrian Hunter adrian.hunter at intel.com
Mon Mar 3 02:44:33 PST 2025


On 17/02/25 21:58, Leo Yan wrote:
> Add a check for the generated string of flags.  Print out the raw number
> if the string generation fails.

How does it fail?

> 
> In another case, if the string length is longer than the aligned size,
> allow the completed string to be printed.
> 
> Reviewed-by: Ian Rogers <irogers at google.com>
> Reviewed-by: James Clark <james.clark at linaro.org>
> Signed-off-by: Leo Yan <leo.yan at arm.com>
> ---
>  tools/perf/builtin-script.c   | 10 ++++++++--
>  tools/perf/util/trace-event.h |  2 ++
>  2 files changed, 10 insertions(+), 2 deletions(-)
> 
> diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
> index d797cec4f054..2c4b1fb7dc72 100644
> --- a/tools/perf/builtin-script.c
> +++ b/tools/perf/builtin-script.c
> @@ -1709,9 +1709,15 @@ static int perf_sample__fprintf_bts(struct perf_sample *sample,
>  static int perf_sample__fprintf_flags(u32 flags, FILE *fp)
>  {
>  	char str[SAMPLE_FLAGS_BUF_SIZE];
> +	int ret;
> +
> +	ret = perf_sample__sprintf_flags(flags, str, sizeof(str));
> +	if (ret < 0)

AFAICT ret is always >= 0

> +		return fprintf(fp, "  raw flags:0x%-*x ",
> +			       SAMPLE_FLAGS_STR_ALIGNED_SIZE - 12, flags);
>  
> -	perf_sample__sprintf_flags(flags, str, sizeof(str));
> -	return fprintf(fp, "  %-21s ", str);
> +	ret = max(ret, SAMPLE_FLAGS_STR_ALIGNED_SIZE);
> +	return fprintf(fp, "  %-*s ", ret, str);

-21 means the field width is 21 and left-justified.  It should not
truncate the string.

>  }
>  
>  struct printer_data {
> diff --git a/tools/perf/util/trace-event.h b/tools/perf/util/trace-event.h
> index ac9fde2f980c..71e680bc3d4b 100644
> --- a/tools/perf/util/trace-event.h
> +++ b/tools/perf/util/trace-event.h
> @@ -145,6 +145,8 @@ int common_flags(struct scripting_context *context);
>  int common_lock_depth(struct scripting_context *context);
>  
>  #define SAMPLE_FLAGS_BUF_SIZE 64
> +#define SAMPLE_FLAGS_STR_ALIGNED_SIZE	21
> +
>  int perf_sample__sprintf_flags(u32 flags, char *str, size_t sz);
>  
>  #if defined(LIBTRACEEVENT_VERSION) &&  LIBTRACEEVENT_VERSION >= MAKE_LIBTRACEEVENT_VERSION(1, 5, 0)




More information about the linux-arm-kernel mailing list