perf script: Make printing flags reliable
authorLeo Yan <leo.yan@arm.com>
Tue, 4 Mar 2025 11:12:29 +0000 (11:12 +0000)
committerNamhyung Kim <namhyung@kernel.org>
Wed, 5 Mar 2025 17:13:19 +0000 (09:13 -0800)
Add a check for the generated string of flags.  Print out the raw number
if the string generation fails.

Use the SAMPLE_FLAGS_STR_ALIGNED_SIZE macro to replace the value '21'.

Reviewed-by: Ian Rogers <irogers@google.com>
Reviewed-by: James Clark <james.clark@linaro.org>
Signed-off-by: Leo Yan <leo.yan@arm.com>
Reviewed-by: Adrian Hunter <adrian.hunter@intel.com>
Link: https://lore.kernel.org/r/20250304111240.3378214-2-leo.yan@arm.com
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
tools/perf/builtin-script.c
tools/perf/util/trace-event.h

index d797cec4f054cc703f8a691774e22f8aab0cdb36..8ca04293d335dd0aca5eb6d2fae2c9848153855e 100644 (file)
@@ -1709,9 +1709,14 @@ 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)
+               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);
+       return fprintf(fp, "  %-*s ", SAMPLE_FLAGS_STR_ALIGNED_SIZE, str);
 }
 
 struct printer_data {
index ac9fde2f980cc31bcadbe92e2b4ac0003fa23184..71e680bc3d4bde1f89c82f72f027a6dce9ce0330 100644 (file)
@@ -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)