aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArnaldo Carvalho de Melo <acme@redhat.com>2012-03-14 11:29:29 -0400
committerArnaldo Carvalho de Melo <acme@redhat.com>2012-03-14 11:36:19 -0400
commite7f01d1e3d8d501deb8abeaa269d5d48a703b8b0 (patch)
tree2746465309fd16392ed7c6265dea53366c6e52c7
parentb832796caa1fda8516464a003c8c7cc547bc20c2 (diff)
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 <anton@samba.org> Cc: Anton Blanchard <anton@samba.org> Cc: Eric B Munson <emunson@mgebm.net> Cc: David Ahern <dsahern@gmail.com> Cc: Frederic Weisbecker <fweisbec@gmail.com> Cc: Mike Galbraith <efault@gmx.de> Cc: Paul Mackerras <paulus@samba.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Stephane Eranian <eranian@google.com> Cc: Yanmin Zhang <yanmin_zhang@linux.intel.com> Cc: stable@kernel.org Link: http://lkml.kernel.org/n/tip-kwxo2eh29cxmd8ilixi2005x@git.kernel.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
-rw-r--r--tools/perf/arch/powerpc/util/header.c2
-rw-r--r--tools/perf/arch/x86/util/header.c2
-rw-r--r--tools/perf/util/color.c9
-rw-r--r--tools/perf/util/header.c4
-rw-r--r--tools/perf/util/hist.c30
-rw-r--r--tools/perf/util/strbuf.c7
-rw-r--r--tools/perf/util/ui/browsers/hists.c12
-rw-r--r--tools/perf/util/ui/helpline.c2
8 files changed, 35 insertions, 33 deletions
diff --git a/tools/perf/arch/powerpc/util/header.c b/tools/perf/arch/powerpc/util/header.c
index eba80c292945..2f7073d107fd 100644
--- a/tools/perf/arch/powerpc/util/header.c
+++ b/tools/perf/arch/powerpc/util/header.c
@@ -25,7 +25,7 @@ get_cpuid(char *buffer, size_t sz)
25 25
26 pvr = mfspr(SPRN_PVR); 26 pvr = mfspr(SPRN_PVR);
27 27
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));
29 29
30 /* look for end marker to ensure the entire data fit */ 30 /* look for end marker to ensure the entire data fit */
31 if (strchr(buffer, '$')) { 31 if (strchr(buffer, '$')) {
diff --git a/tools/perf/arch/x86/util/header.c b/tools/perf/arch/x86/util/header.c
index f94006068d2b..146d12a1cec0 100644
--- a/tools/perf/arch/x86/util/header.c
+++ b/tools/perf/arch/x86/util/header.c
@@ -48,7 +48,7 @@ get_cpuid(char *buffer, size_t sz)
48 if (family >= 0x6) 48 if (family >= 0x6)
49 model += ((a >> 16) & 0xf) << 4; 49 model += ((a >> 16) & 0xf) << 4;
50 } 50 }
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);
52 52
53 /* look for end marker to ensure the entire data fit */ 53 /* look for end marker to ensure the entire data fit */
54 if (strchr(buffer, '$')) { 54 if (strchr(buffer, '$')) {
diff --git a/tools/perf/util/color.c b/tools/perf/util/color.c
index 521c38a79190..11e46da17bbb 100644
--- a/tools/perf/util/color.c
+++ b/tools/perf/util/color.c
@@ -1,3 +1,4 @@
1#include <linux/kernel.h>
1#include "cache.h" 2#include "cache.h"
2#include "color.h" 3#include "color.h"
3 4
@@ -182,12 +183,12 @@ static int __color_vsnprintf(char *bf, size_t size, const char *color,
182 } 183 }
183 184
184 if (perf_use_color_default && *color) 185 if (perf_use_color_default && *color)
185 r += snprintf(bf, size, "%s", color); 186 r += scnprintf(bf, size, "%s", color);
186 r += vsnprintf(bf + r, size - r, fmt, args); 187 r += vscnprintf(bf + r, size - r, fmt, args);
187 if (perf_use_color_default && *color) 188 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);
189 if (trail) 190 if (trail)
190 r += snprintf(bf + r, size - r, "%s", trail); 191 r += scnprintf(bf + r, size - r, "%s", trail);
191 return r; 192 return r;
192} 193}
193 194
diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
index ecd7f4dd7eea..14bb035c5fd9 100644
--- a/tools/perf/util/header.c
+++ b/tools/perf/util/header.c
@@ -280,7 +280,7 @@ int build_id_cache__add_s(const char *sbuild_id, const char *debugdir,
280 if (realname == NULL || filename == NULL || linkname == NULL) 280 if (realname == NULL || filename == NULL || linkname == NULL)
281 goto out_free; 281 goto out_free;
282 282
283 len = snprintf(filename, size, "%s%s%s", 283 len = scnprintf(filename, size, "%s%s%s",
284 debugdir, is_kallsyms ? "/" : "", realname); 284 debugdir, is_kallsyms ? "/" : "", realname);
285 if (mkdir_p(filename, 0755)) 285 if (mkdir_p(filename, 0755))
286 goto out_free; 286 goto out_free;
@@ -295,7 +295,7 @@ int build_id_cache__add_s(const char *sbuild_id, const char *debugdir,
295 goto out_free; 295 goto out_free;
296 } 296 }
297 297
298 len = snprintf(linkname, size, "%s/.build-id/%.2s", 298 len = scnprintf(linkname, size, "%s/.build-id/%.2s",
299 debugdir, sbuild_id); 299 debugdir, sbuild_id);
300 300
301 if (access(linkname, X_OK) && mkdir_p(linkname, 0755)) 301 if (access(linkname, X_OK) && mkdir_p(linkname, 0755))
diff --git a/tools/perf/util/hist.c b/tools/perf/util/hist.c
index 6f505d1abac7..e11e482bd185 100644
--- a/tools/perf/util/hist.c
+++ b/tools/perf/util/hist.c
@@ -768,7 +768,7 @@ static int hist_entry__pcnt_snprintf(struct hist_entry *he, char *s,
768 sep ? "%.2f" : " %6.2f%%", 768 sep ? "%.2f" : " %6.2f%%",
769 (period * 100.0) / total); 769 (period * 100.0) / total);
770 else 770 else
771 ret = snprintf(s, size, sep ? "%.2f" : " %6.2f%%", 771 ret = scnprintf(s, size, sep ? "%.2f" : " %6.2f%%",
772 (period * 100.0) / total); 772 (period * 100.0) / total);
773 if (symbol_conf.show_cpu_utilization) { 773 if (symbol_conf.show_cpu_utilization) {
774 ret += percent_color_snprintf(s + ret, size - ret, 774 ret += percent_color_snprintf(s + ret, size - ret,
@@ -791,20 +791,20 @@ static int hist_entry__pcnt_snprintf(struct hist_entry *he, char *s,
791 } 791 }
792 } 792 }
793 } else 793 } else
794 ret = snprintf(s, size, sep ? "%" PRIu64 : "%12" PRIu64 " ", period); 794 ret = scnprintf(s, size, sep ? "%" PRIu64 : "%12" PRIu64 " ", period);
795 795
796 if (symbol_conf.show_nr_samples) { 796 if (symbol_conf.show_nr_samples) {
797 if (sep) 797 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);
799 else 799 else
800 ret += snprintf(s + ret, size - ret, "%11" PRIu64, nr_events); 800 ret += scnprintf(s + ret, size - ret, "%11" PRIu64, nr_events);
801 } 801 }
802 802
803 if (symbol_conf.show_total_period) { 803 if (symbol_conf.show_total_period) {
804 if (sep) 804 if (sep)
805 ret += snprintf(s + ret, size - ret, "%c%" PRIu64, *sep, period); 805 ret += scnprintf(s + ret, size - ret, "%c%" PRIu64, *sep, period);
806 else 806 else
807 ret += snprintf(s + ret, size - ret, " %12" PRIu64, period); 807 ret += scnprintf(s + ret, size - ret, " %12" PRIu64, period);
808 } 808 }
809 809
810 if (pair_hists) { 810 if (pair_hists) {
@@ -819,25 +819,25 @@ static int hist_entry__pcnt_snprintf(struct hist_entry *he, char *s,
819 diff = new_percent - old_percent; 819 diff = new_percent - old_percent;
820 820
821 if (fabs(diff) >= 0.01) 821 if (fabs(diff) >= 0.01)
822 snprintf(bf, sizeof(bf), "%+4.2F%%", diff); 822 ret += scnprintf(bf, sizeof(bf), "%+4.2F%%", diff);
823 else 823 else
824 snprintf(bf, sizeof(bf), " "); 824 ret += scnprintf(bf, sizeof(bf), " ");
825 825
826 if (sep) 826 if (sep)
827 ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf); 827 ret += scnprintf(s + ret, size - ret, "%c%s", *sep, bf);
828 else 828 else
829 ret += snprintf(s + ret, size - ret, "%11.11s", bf); 829 ret += scnprintf(s + ret, size - ret, "%11.11s", bf);
830 830
831 if (show_displacement) { 831 if (show_displacement) {
832 if (displacement) 832 if (displacement)
833 snprintf(bf, sizeof(bf), "%+4ld", displacement); 833 ret += scnprintf(bf, sizeof(bf), "%+4ld", displacement);
834 else 834 else
835 snprintf(bf, sizeof(bf), " "); 835 ret += scnprintf(bf, sizeof(bf), " ");
836 836
837 if (sep) 837 if (sep)
838 ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf); 838 ret += scnprintf(s + ret, size - ret, "%c%s", *sep, bf);
839 else 839 else
840 ret += snprintf(s + ret, size - ret, "%6.6s", bf); 840 ret += scnprintf(s + ret, size - ret, "%6.6s", bf);
841 } 841 }
842 } 842 }
843 843
@@ -855,7 +855,7 @@ int hist_entry__snprintf(struct hist_entry *he, char *s, size_t size,
855 if (se->elide) 855 if (se->elide)
856 continue; 856 continue;
857 857
858 ret += snprintf(s + ret, size - ret, "%s", sep ?: " "); 858 ret += scnprintf(s + ret, size - ret, "%s", sep ?: " ");
859 ret += se->se_snprintf(he, s + ret, size - ret, 859 ret += se->se_snprintf(he, s + ret, size - ret,
860 hists__col_len(hists, se->se_width_idx)); 860 hists__col_len(hists, se->se_width_idx));
861 } 861 }
diff --git a/tools/perf/util/strbuf.c b/tools/perf/util/strbuf.c
index 92e068517c1a..2eeb51baf077 100644
--- a/tools/perf/util/strbuf.c
+++ b/tools/perf/util/strbuf.c
@@ -1,4 +1,5 @@
1#include "cache.h" 1#include "cache.h"
2#include <linux/kernel.h>
2 3
3int prefixcmp(const char *str, const char *prefix) 4int prefixcmp(const char *str, const char *prefix)
4{ 5{
@@ -89,14 +90,14 @@ void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
89 if (!strbuf_avail(sb)) 90 if (!strbuf_avail(sb))
90 strbuf_grow(sb, 64); 91 strbuf_grow(sb, 64);
91 va_start(ap, fmt); 92 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);
93 va_end(ap); 94 va_end(ap);
94 if (len < 0) 95 if (len < 0)
95 die("your vsnprintf is broken"); 96 die("your vscnprintf is broken");
96 if (len > strbuf_avail(sb)) { 97 if (len > strbuf_avail(sb)) {
97 strbuf_grow(sb, len); 98 strbuf_grow(sb, len);
98 va_start(ap, fmt); 99 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);
100 va_end(ap); 101 va_end(ap);
101 if (len > strbuf_avail(sb)) { 102 if (len > strbuf_avail(sb)) {
102 die("this should not happen, your snprintf is broken"); 103 die("this should not happen, your snprintf is broken");
diff --git a/tools/perf/util/ui/browsers/hists.c b/tools/perf/util/ui/browsers/hists.c
index e81aef1f2569..bb9197c9c4a4 100644
--- a/tools/perf/util/ui/browsers/hists.c
+++ b/tools/perf/util/ui/browsers/hists.c
@@ -837,15 +837,15 @@ static int hists__browser_title(struct hists *self, char *bf, size_t size,
837 unsigned long nr_events = self->stats.nr_events[PERF_RECORD_SAMPLE]; 837 unsigned long nr_events = self->stats.nr_events[PERF_RECORD_SAMPLE];
838 838
839 nr_events = convert_unit(nr_events, &unit); 839 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);
841 841
842 if (thread) 842 if (thread)
843 printed += snprintf(bf + printed, size - printed, 843 printed += scnprintf(bf + printed, size - printed,
844 ", Thread: %s(%d)", 844 ", Thread: %s(%d)",
845 (thread->comm_set ? thread->comm : ""), 845 (thread->comm_set ? thread->comm : ""),
846 thread->pid); 846 thread->pid);
847 if (dso) 847 if (dso)
848 printed += snprintf(bf + printed, size - printed, 848 printed += scnprintf(bf + printed, size - printed,
849 ", DSO: %s", dso->short_name); 849 ", DSO: %s", dso->short_name);
850 return printed; 850 return printed;
851} 851}
@@ -1095,7 +1095,7 @@ static void perf_evsel_menu__write(struct ui_browser *browser,
1095 HE_COLORSET_NORMAL); 1095 HE_COLORSET_NORMAL);
1096 1096
1097 nr_events = convert_unit(nr_events, &unit); 1097 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,
1099 unit, unit == ' ' ? "" : " ", ev_name); 1099 unit, unit == ' ' ? "" : " ", ev_name);
1100 slsmg_printf("%s", bf); 1100 slsmg_printf("%s", bf);
1101 1101
@@ -1105,8 +1105,8 @@ static void perf_evsel_menu__write(struct ui_browser *browser,
1105 if (!current_entry) 1105 if (!current_entry)
1106 ui_browser__set_color(browser, HE_COLORSET_TOP); 1106 ui_browser__set_color(browser, HE_COLORSET_TOP);
1107 nr_events = convert_unit(nr_events, &unit); 1107 nr_events = convert_unit(nr_events, &unit);
1108 snprintf(bf, sizeof(bf), ": %ld%c%schunks LOST!", nr_events, 1108 printed += scnprintf(bf, sizeof(bf), ": %ld%c%schunks LOST!",
1109 unit, unit == ' ' ? "" : " "); 1109 nr_events, unit, unit == ' ' ? "" : " ");
1110 warn = bf; 1110 warn = bf;
1111 } 1111 }
1112 1112
diff --git a/tools/perf/util/ui/helpline.c b/tools/perf/util/ui/helpline.c
index 4f48f5901b30..2f950c2641c8 100644
--- a/tools/perf/util/ui/helpline.c
+++ b/tools/perf/util/ui/helpline.c
@@ -64,7 +64,7 @@ int ui_helpline__show_help(const char *format, va_list ap)
64 static int backlog; 64 static int backlog;
65 65
66 pthread_mutex_lock(&ui__lock); 66 pthread_mutex_lock(&ui__lock);
67 ret = vsnprintf(ui_helpline__last_msg + backlog, 67 ret = vscnprintf(ui_helpline__last_msg + backlog,
68 sizeof(ui_helpline__last_msg) - backlog, format, ap); 68 sizeof(ui_helpline__last_msg) - backlog, format, ap);
69 backlog += ret; 69 backlog += ret;
70 70