Skip to content

Commit e7f01d1

Browse files
committed
perf tools: Use scnprintf where applicable
Several places were expecting that the value returned was the number of characters printed, not what would be printed if there was space. Fix it by using the scnprintf and vscnprintf variants we inherited from the kernel sources. Some corner cases where the number of printed characters were not accounted were fixed too. Reported-by: Anton Blanchard <[email protected]> Cc: Anton Blanchard <[email protected]> Cc: Eric B Munson <[email protected]> Cc: David Ahern <[email protected]> Cc: Frederic Weisbecker <[email protected]> Cc: Mike Galbraith <[email protected]> Cc: Paul Mackerras <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Stephane Eranian <[email protected]> Cc: Yanmin Zhang <[email protected]> Cc: [email protected] Link: http://lkml.kernel.org/n/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent b832796 commit e7f01d1

File tree

8 files changed

+35
-33
lines changed

8 files changed

+35
-33
lines changed

tools/perf/arch/powerpc/util/header.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ get_cpuid(char *buffer, size_t sz)
2525

2626
pvr = mfspr(SPRN_PVR);
2727

28-
nb = snprintf(buffer, sz, "%lu,%lu$", PVR_VER(pvr), PVR_REV(pvr));
28+
nb = scnprintf(buffer, sz, "%lu,%lu$", PVR_VER(pvr), PVR_REV(pvr));
2929

3030
/* look for end marker to ensure the entire data fit */
3131
if (strchr(buffer, '$')) {

tools/perf/arch/x86/util/header.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ get_cpuid(char *buffer, size_t sz)
4848
if (family >= 0x6)
4949
model += ((a >> 16) & 0xf) << 4;
5050
}
51-
nb = snprintf(buffer, sz, "%s,%u,%u,%u$", vendor, family, model, step);
51+
nb = scnprintf(buffer, sz, "%s,%u,%u,%u$", vendor, family, model, step);
5252

5353
/* look for end marker to ensure the entire data fit */
5454
if (strchr(buffer, '$')) {

tools/perf/util/color.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <linux/kernel.h>
12
#include "cache.h"
23
#include "color.h"
34

@@ -182,12 +183,12 @@ static int __color_vsnprintf(char *bf, size_t size, const char *color,
182183
}
183184

184185
if (perf_use_color_default && *color)
185-
r += snprintf(bf, size, "%s", color);
186-
r += vsnprintf(bf + r, size - r, fmt, args);
186+
r += scnprintf(bf, size, "%s", color);
187+
r += vscnprintf(bf + r, size - r, fmt, args);
187188
if (perf_use_color_default && *color)
188-
r += snprintf(bf + r, size - r, "%s", PERF_COLOR_RESET);
189+
r += scnprintf(bf + r, size - r, "%s", PERF_COLOR_RESET);
189190
if (trail)
190-
r += snprintf(bf + r, size - r, "%s", trail);
191+
r += scnprintf(bf + r, size - r, "%s", trail);
191192
return r;
192193
}
193194

tools/perf/util/header.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ int build_id_cache__add_s(const char *sbuild_id, const char *debugdir,
280280
if (realname == NULL || filename == NULL || linkname == NULL)
281281
goto out_free;
282282

283-
len = snprintf(filename, size, "%s%s%s",
283+
len = scnprintf(filename, size, "%s%s%s",
284284
debugdir, is_kallsyms ? "/" : "", realname);
285285
if (mkdir_p(filename, 0755))
286286
goto out_free;
@@ -295,7 +295,7 @@ int build_id_cache__add_s(const char *sbuild_id, const char *debugdir,
295295
goto out_free;
296296
}
297297

298-
len = snprintf(linkname, size, "%s/.build-id/%.2s",
298+
len = scnprintf(linkname, size, "%s/.build-id/%.2s",
299299
debugdir, sbuild_id);
300300

301301
if (access(linkname, X_OK) && mkdir_p(linkname, 0755))

tools/perf/util/hist.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ static int hist_entry__pcnt_snprintf(struct hist_entry *he, char *s,
768768
sep ? "%.2f" : " %6.2f%%",
769769
(period * 100.0) / total);
770770
else
771-
ret = snprintf(s, size, sep ? "%.2f" : " %6.2f%%",
771+
ret = scnprintf(s, size, sep ? "%.2f" : " %6.2f%%",
772772
(period * 100.0) / total);
773773
if (symbol_conf.show_cpu_utilization) {
774774
ret += percent_color_snprintf(s + ret, size - ret,
@@ -791,20 +791,20 @@ static int hist_entry__pcnt_snprintf(struct hist_entry *he, char *s,
791791
}
792792
}
793793
} else
794-
ret = snprintf(s, size, sep ? "%" PRIu64 : "%12" PRIu64 " ", period);
794+
ret = scnprintf(s, size, sep ? "%" PRIu64 : "%12" PRIu64 " ", period);
795795

796796
if (symbol_conf.show_nr_samples) {
797797
if (sep)
798-
ret += snprintf(s + ret, size - ret, "%c%" PRIu64, *sep, nr_events);
798+
ret += scnprintf(s + ret, size - ret, "%c%" PRIu64, *sep, nr_events);
799799
else
800-
ret += snprintf(s + ret, size - ret, "%11" PRIu64, nr_events);
800+
ret += scnprintf(s + ret, size - ret, "%11" PRIu64, nr_events);
801801
}
802802

803803
if (symbol_conf.show_total_period) {
804804
if (sep)
805-
ret += snprintf(s + ret, size - ret, "%c%" PRIu64, *sep, period);
805+
ret += scnprintf(s + ret, size - ret, "%c%" PRIu64, *sep, period);
806806
else
807-
ret += snprintf(s + ret, size - ret, " %12" PRIu64, period);
807+
ret += scnprintf(s + ret, size - ret, " %12" PRIu64, period);
808808
}
809809

810810
if (pair_hists) {
@@ -819,25 +819,25 @@ static int hist_entry__pcnt_snprintf(struct hist_entry *he, char *s,
819819
diff = new_percent - old_percent;
820820

821821
if (fabs(diff) >= 0.01)
822-
snprintf(bf, sizeof(bf), "%+4.2F%%", diff);
822+
ret += scnprintf(bf, sizeof(bf), "%+4.2F%%", diff);
823823
else
824-
snprintf(bf, sizeof(bf), " ");
824+
ret += scnprintf(bf, sizeof(bf), " ");
825825

826826
if (sep)
827-
ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
827+
ret += scnprintf(s + ret, size - ret, "%c%s", *sep, bf);
828828
else
829-
ret += snprintf(s + ret, size - ret, "%11.11s", bf);
829+
ret += scnprintf(s + ret, size - ret, "%11.11s", bf);
830830

831831
if (show_displacement) {
832832
if (displacement)
833-
snprintf(bf, sizeof(bf), "%+4ld", displacement);
833+
ret += scnprintf(bf, sizeof(bf), "%+4ld", displacement);
834834
else
835-
snprintf(bf, sizeof(bf), " ");
835+
ret += scnprintf(bf, sizeof(bf), " ");
836836

837837
if (sep)
838-
ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
838+
ret += scnprintf(s + ret, size - ret, "%c%s", *sep, bf);
839839
else
840-
ret += snprintf(s + ret, size - ret, "%6.6s", bf);
840+
ret += scnprintf(s + ret, size - ret, "%6.6s", bf);
841841
}
842842
}
843843

@@ -855,7 +855,7 @@ int hist_entry__snprintf(struct hist_entry *he, char *s, size_t size,
855855
if (se->elide)
856856
continue;
857857

858-
ret += snprintf(s + ret, size - ret, "%s", sep ?: " ");
858+
ret += scnprintf(s + ret, size - ret, "%s", sep ?: " ");
859859
ret += se->se_snprintf(he, s + ret, size - ret,
860860
hists__col_len(hists, se->se_width_idx));
861861
}

tools/perf/util/strbuf.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "cache.h"
2+
#include <linux/kernel.h>
23

34
int prefixcmp(const char *str, const char *prefix)
45
{
@@ -89,14 +90,14 @@ void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
8990
if (!strbuf_avail(sb))
9091
strbuf_grow(sb, 64);
9192
va_start(ap, fmt);
92-
len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
93+
len = vscnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
9394
va_end(ap);
9495
if (len < 0)
95-
die("your vsnprintf is broken");
96+
die("your vscnprintf is broken");
9697
if (len > strbuf_avail(sb)) {
9798
strbuf_grow(sb, len);
9899
va_start(ap, fmt);
99-
len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
100+
len = vscnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
100101
va_end(ap);
101102
if (len > strbuf_avail(sb)) {
102103
die("this should not happen, your snprintf is broken");

tools/perf/util/ui/browsers/hists.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -837,15 +837,15 @@ static int hists__browser_title(struct hists *self, char *bf, size_t size,
837837
unsigned long nr_events = self->stats.nr_events[PERF_RECORD_SAMPLE];
838838

839839
nr_events = convert_unit(nr_events, &unit);
840-
printed = snprintf(bf, size, "Events: %lu%c %s", nr_events, unit, ev_name);
840+
printed = scnprintf(bf, size, "Events: %lu%c %s", nr_events, unit, ev_name);
841841

842842
if (thread)
843-
printed += snprintf(bf + printed, size - printed,
843+
printed += scnprintf(bf + printed, size - printed,
844844
", Thread: %s(%d)",
845845
(thread->comm_set ? thread->comm : ""),
846846
thread->pid);
847847
if (dso)
848-
printed += snprintf(bf + printed, size - printed,
848+
printed += scnprintf(bf + printed, size - printed,
849849
", DSO: %s", dso->short_name);
850850
return printed;
851851
}
@@ -1095,7 +1095,7 @@ static void perf_evsel_menu__write(struct ui_browser *browser,
10951095
HE_COLORSET_NORMAL);
10961096

10971097
nr_events = convert_unit(nr_events, &unit);
1098-
printed = snprintf(bf, sizeof(bf), "%lu%c%s%s", nr_events,
1098+
printed = scnprintf(bf, sizeof(bf), "%lu%c%s%s", nr_events,
10991099
unit, unit == ' ' ? "" : " ", ev_name);
11001100
slsmg_printf("%s", bf);
11011101

@@ -1105,8 +1105,8 @@ static void perf_evsel_menu__write(struct ui_browser *browser,
11051105
if (!current_entry)
11061106
ui_browser__set_color(browser, HE_COLORSET_TOP);
11071107
nr_events = convert_unit(nr_events, &unit);
1108-
snprintf(bf, sizeof(bf), ": %ld%c%schunks LOST!", nr_events,
1109-
unit, unit == ' ' ? "" : " ");
1108+
printed += scnprintf(bf, sizeof(bf), ": %ld%c%schunks LOST!",
1109+
nr_events, unit, unit == ' ' ? "" : " ");
11101110
warn = bf;
11111111
}
11121112

tools/perf/util/ui/helpline.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ int ui_helpline__show_help(const char *format, va_list ap)
6464
static int backlog;
6565

6666
pthread_mutex_lock(&ui__lock);
67-
ret = vsnprintf(ui_helpline__last_msg + backlog,
67+
ret = vscnprintf(ui_helpline__last_msg + backlog,
6868
sizeof(ui_helpline__last_msg) - backlog, format, ap);
6969
backlog += ret;
7070

0 commit comments

Comments
 (0)