perf color: Add printf format checking and resolve issues
authorIan Rogers <irogers@google.com>
Thu, 17 Oct 2024 17:53:50 +0000 (10:53 -0700)
committerNamhyung Kim <namhyung@kernel.org>
Thu, 17 Oct 2024 19:44:26 +0000 (12:44 -0700)
Add printf format checking to vararg printf routines in
color.h. Resolve build errors/bugs that are found through this
checking.

Signed-off-by: Ian Rogers <irogers@google.com>
Cc: Yicong Yang <yangyicong@hisilicon.com>
Cc: Weilin Wang <weilin.wang@intel.com>
Cc: Will Deacon <will@kernel.org>
Cc: James Clark <james.clark@linaro.org>
Cc: Mike Leach <mike.leach@linaro.org>
Cc: Leo Yan <leo.yan@linux.dev>
Cc: Sumanth Korikkar <sumanthk@linux.ibm.com>
Cc: Thomas Richter <tmricht@linux.ibm.com>
Cc: Tim Chen <tim.c.chen@linux.intel.com>
Cc: John Garry <john.g.garry@oracle.com>
Link: https://lore.kernel.org/r/20241017175356.783793-2-irogers@google.com
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
tools/perf/builtin-sched.c
tools/perf/builtin-trace.c
tools/perf/util/arm-spe.c
tools/perf/util/color.h
tools/perf/util/intel-bts.c
tools/perf/util/intel-pt.c
tools/perf/util/s390-cpumsf.c
tools/perf/util/s390-sample-raw.c

index c053074b98b419412027fca43a853a3f2794b0e1..7049c60ebf77c6b121ada6d05c2cb84459864487 100644 (file)
@@ -1743,7 +1743,7 @@ static int map_switch_event(struct perf_sched *sched, struct evsel *evsel,
        }
 
        if (sched->map.comp && new_cpu)
-               color_fprintf(stdout, color, " (CPU %d)", this_cpu);
+               color_fprintf(stdout, color, " (CPU %d)", this_cpu.cpu);
 
        if (proceed != 1) {
                color_fprintf(stdout, color, "\n");
index 0b30297b4ecb3f68036a416de6d79e807d2f500c..748b061f8678df22550eb4c848ce3128c4c0306d 100644 (file)
@@ -1873,7 +1873,7 @@ static int trace__process_event(struct trace *trace, struct machine *machine,
        switch (event->header.type) {
        case PERF_RECORD_LOST:
                color_fprintf(trace->output, PERF_COLOR_RED,
-                             "LOST %" PRIu64 " events!\n", event->lost.lost);
+                             "LOST %" PRIu64 " events!\n", (u64)event->lost.lost);
                ret = machine__process_lost_event(machine, event, sample);
                break;
        default:
index c424e5b9a11d9fcd61794bdeb4e32a19e5b6a48f..856cc36f33d79184c51d6d327fadb4b79cbb01da 100644 (file)
@@ -122,7 +122,7 @@ static void arm_spe_dump(struct arm_spe *spe __maybe_unused,
                else
                        pkt_len = 1;
                printf(".");
-               color_fprintf(stdout, color, "  %08x: ", pos);
+               color_fprintf(stdout, color, "  %08zx: ", pos);
                for (i = 0; i < pkt_len; i++)
                        color_fprintf(stdout, color, " %02x", buf[i]);
                for (; i < 16; i++)
index aecf56dae73f532a24bdfe57acd90757afe706eb..b2f37de305f637888eca80e5a9252777a0bea038 100644 (file)
@@ -2,6 +2,7 @@
 #ifndef __PERF_COLOR_H
 #define __PERF_COLOR_H
 
+#include <linux/compiler.h>
 #include <stdio.h>
 #include <stdarg.h>
 
@@ -37,11 +38,11 @@ int perf_config_colorbool(const char *var, const char *value, int stdout_is_tty)
 int color_vsnprintf(char *bf, size_t size, const char *color,
                    const char *fmt, va_list args);
 int color_vfprintf(FILE *fp, const char *color, const char *fmt, va_list args);
-int color_fprintf(FILE *fp, const char *color, const char *fmt, ...);
-int color_snprintf(char *bf, size_t size, const char *color, const char *fmt, ...);
+int color_fprintf(FILE *fp, const char *color, const char *fmt, ...) __printf(3, 4);
+int color_snprintf(char *bf, size_t size, const char *color, const char *fmt, ...) __printf(4, 5);
 int value_color_snprintf(char *bf, size_t size, const char *fmt, double value);
-int percent_color_snprintf(char *bf, size_t size, const char *fmt, ...);
-int percent_color_len_snprintf(char *bf, size_t size, const char *fmt, ...);
+int percent_color_snprintf(char *bf, size_t size, const char *fmt, ...) __printf(3, 4);
+int percent_color_len_snprintf(char *bf, size_t size, const char *fmt, ...) __printf(3, 4);
 int percent_color_fprintf(FILE *fp, const char *fmt, double percent);
 const char *get_percent_color(double percent);
 
index 27d9b5c9fec8becb2d31b91193fd3b1832592d4c..3ea82d5e8d2ee8e402db508e98a760df0d67d95a 100644 (file)
@@ -100,7 +100,7 @@ static void intel_bts_dump(struct intel_bts *bts __maybe_unused,
                else
                        sz = len;
                printf(".");
-               color_fprintf(stdout, color, "  %08x: ", pos);
+               color_fprintf(stdout, color, "  %08zx: ", pos);
                for (i = 0; i < sz; i++)
                        color_fprintf(stdout, color, " %02x", buf[i]);
                for (; i < br_sz; i++)
index fd2597613f3dcb125654cf2a620b9416f13a08b2..3fe67bf652b647bb89e53aab41f6ad61beb23725 100644 (file)
@@ -249,7 +249,7 @@ static void intel_pt_dump(struct intel_pt *pt __maybe_unused,
                else
                        pkt_len = 1;
                printf(".");
-               color_fprintf(stdout, color, "  %08x: ", pos);
+               color_fprintf(stdout, color, "  %08zx: ", pos);
                for (i = 0; i < pkt_len; i++)
                        color_fprintf(stdout, color, " %02x", buf[i]);
                for (; i < 16; i++)
index 73846b73d0cf4c25451dccde419cb0c87cbedbba..30638653ad2d65e1a629e0e1c11d071afad89bae 100644 (file)
@@ -345,7 +345,7 @@ static bool s390_cpumsf_trailer_show(const char *color, size_t pos,
        }
        color_fprintf(stdout, color, "    [%#08zx] Trailer %c%c%c bsdes:%d"
                      " dsdes:%d Overflow:%lld Time:%#llx\n"
-                     "\t\tC:%d TOD:%#lx\n",
+                     "\t\tC:%d TOD:%#llx\n",
                      pos,
                      te->f ? 'F' : ' ',
                      te->a ? 'A' : ' ',
index 53383e97ec9d5731276fcce0021ec6a5176196c5..335217bb532b427b010cff7573435bb5b7e7fb9e 100644 (file)
@@ -98,12 +98,12 @@ static void s390_cpumcfdg_dumptrail(const char *color, size_t offset,
        te.res2 = be32_to_cpu(tep->res2);
 
        color_fprintf(stdout, color, "    [%#08zx] Trailer:%c%c%c%c%c"
-                     " Cfvn:%d Csvn:%d Speed:%d TOD:%#llx\n",
+                     " Cfvn:%d Csvn:%d Speed:%d TOD:%#lx\n",
                      offset, te.clock_base ? 'T' : ' ',
                      te.speed ? 'S' : ' ', te.mtda ? 'M' : ' ',
                      te.caca ? 'C' : ' ', te.lcda ? 'L' : ' ',
                      te.cfvn, te.csvn, te.cpu_speed, te.timestamp);
-       color_fprintf(stdout, color, "\t\t1:%lx 2:%lx 3:%lx TOD-Base:%#llx"
+       color_fprintf(stdout, color, "\t\t1:%lx 2:%lx 3:%lx TOD-Base:%#lx"
                      " Type:%x\n\n",
                      te.progusage1, te.progusage2, te.progusage3,
                      te.tod_base, te.mach_type);
@@ -205,7 +205,7 @@ static void s390_cpumcfdg_dump(struct perf_pmu *pmu, struct perf_sample *sample)
                        char *ev_name = get_counter_name(ce.set, i, pmu);
 
                        color_fprintf(stdout, color,
-                                     "\tCounter:%03d %s Value:%#018lx\n", i,
+                                     "\tCounter:%03zd %s Value:%#018"PRIx64"\n", i,
                                      ev_name ?: "<unknown>", be64_to_cpu(*p));
                        free(ev_name);
                }
@@ -260,7 +260,7 @@ static void s390_pai_all_dump(struct evsel *evsel, struct perf_sample *sample)
 
                ev_name = get_counter_name(evsel->core.attr.config,
                                           pai_data.event_nr, evsel->pmu);
-               color_fprintf(stdout, color, "\tCounter:%03d %s Value:%#018lx\n",
+               color_fprintf(stdout, color, "\tCounter:%03d %s Value:%#018"PRIx64"\n",
                              pai_data.event_nr, ev_name ?: "<unknown>",
                              pai_data.event_val);
                free(ev_name);