Skip to contentSkip to conversation

perf tools: runtime fixes for Android

PATCH Superseded Irina Tirdea 6 participants 17 messages Sep 13, 2012 – Sep 27, 2012 perf events

lore
Share & tools
lore
#0 lore raw

cover perf tools: runtime fixes for Android

This is a set of patches with runtime fixes for Android.

Any comments and suggestions are wellcome.

Thanks,
Irina

Irina Tirdea (4):
perf tools: remove sscanf extension %as
perf stat: add compile-time option to disable --big-num perf archive: remove -f from the rm command perf archive: make f the last parameter for tar

 tools/perf/Makefile                 |    8 ++++++++
 tools/perf/builtin-stat.c           |    8 ++++++++
 tools/perf/config/feature-tests.mak |   12 ++++++++++++
 tools/perf/perf-archive.sh          |    6 +++---
 tools/perf/util/probe-event.c       |   25 ++++++++++++++++++-------
 tools/perf/util/trace-event-parse.c |   18 ++++++++----------
 6 files changed, 57 insertions(+), 20 deletions(-)
Signature · 1 line
1.7.9.5
#1 lore raw

PATCH 1/4 perf tools: remove sscanf extension %as

From: Irina Tirdea <[email protected]>

perf uses sscanf extension %as to read and allocate a string in the same step. This is a non-standard extension only present in new versions of glibc.

Replacing the use of sscanf and %as with strtok_r calls in order to parse a given string into its components. This is needed in Android since bionic does not support %as extension for sscanf.

---
 tools/perf/util/probe-event.c       |   25 ++++++++++++++++++-------
 tools/perf/util/trace-event-parse.c |   18 ++++++++----------
 2 files changed, 26 insertions(+), 17 deletions(-)
tools/perf/util/probe-event.c +18 -7
index 4ce04c2..685ddcf 100644
@@ -1100,6 +1100,7 @@ static int parse_probe_trace_command(const char *cmd,
 	struct probe_trace_point *tp = &tev->point;
 	char pr;
 	char *p;
+	char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
 	int ret, i, argc;
 	char **argv;
 
@@ -1116,14 +1117,19 @@ static int parse_probe_trace_command(const char *cmd,
 	}
 
 	/* Scan event and group name. */
-	ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
-		     &pr, (float *)(void *)&tev->group,
-		     (float *)(void *)&tev->event);
-	if (ret != 3) {
+	argv0_str = strdup(argv[0]);
+	fmt1_str = strtok_r(argv0_str, ":", &fmt);
+	fmt2_str = strtok_r(NULL, "/", &fmt);
+	fmt3_str = strtok_r(NULL, " \t", &fmt);
+	if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
+	    || fmt3_str == NULL) {
 		semantic_error("Failed to parse event name: %s\n", argv[0]);
 		ret = -EINVAL;
 		goto out;
 	}
+	pr = fmt1_str[0];
+	tev->group = strdup(fmt2_str);
+	tev->event = strdup(fmt3_str);
 	pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
 
 	tp->retprobe = (pr == 'r');
@@ -1135,10 +1141,13 @@ static int parse_probe_trace_command(const char *cmd,
 		p++;
 	} else
 		p = argv[1];
-	ret = sscanf(p, "%a[^+]+%lu", (float *)(void *)&tp->symbol,
-		     &tp->offset);
-	if (ret == 1)
+	fmt1_str = strtok_r(p, "+", &fmt);
+	tp->symbol = strdup(fmt1_str);
+	fmt2_str = strtok_r(NULL, "", &fmt);
+	if (fmt2_str == NULL)
 		tp->offset = 0;
+	else
+		tp->offset = strtoul(fmt2_str, NULL, 10);
 
 	tev->nargs = argc - 2;
 	tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
@@ -1162,6 +1171,8 @@ static int parse_probe_trace_command(const char *cmd,
 	}
 	ret = 0;
 out:
+	if (argv0_str)
+		free(argv0_str);
 	argv_free(argv);
 	return ret;
 }
tools/perf/util/trace-event-parse.c +8 -10
index aa4c860..3aabcd6 100644
@@ -229,24 +229,22 @@ void parse_proc_kallsyms(struct pevent *pevent,
 	char *next = NULL;
 	char *addr_str;
 	char *mod;
-	char ch;
+	char *fmt;
 
 	line = strtok_r(file, "\n", &next);
 	while (line) {
 		mod = NULL;
-		sscanf(line, "%as %c %as\t[%as",
-		       (float *)(void *)&addr_str, /* workaround gcc warning */
-		       &ch, (float *)(void *)&func, (float *)(void *)&mod);
+		addr_str = strtok_r(line, " ", &fmt);
 		addr = strtoull(addr_str, NULL, 16);
-		free(addr_str);
-
-		/* truncate the extra ']' */
+		/* skip character */
+		strtok_r(NULL, " ", &fmt);
+		func = strtok_r(NULL, "\t", &fmt);
+		mod = strtok_r(NULL, "]", &fmt);
+		/* truncate the extra '[' */
 		if (mod)
-			mod[strlen(mod) - 1] = 0;
+			mod = mod + 1;
 
 		pevent_register_function(pevent, func, addr, mod);
-		free(func);
-		free(mod);
 
 		line = strtok_r(NULL, "\n", &next);
 	}
Signature · 1 line
1.7.9.5
maintainer
#2 lore raw

Re: [PATCH 1/4] perf tools: remove sscanf extension %as

Hi Irina,

(Adding Masami to Cc)

Quoted 30 lines · Irina Tirdea

From: Irina Tirdea <[email protected]>

perf uses sscanf extension %as to read and allocate a string in the same step. This is a non-standard extension only present in new versions of glibc.

Replacing the use of sscanf and %as with strtok_r calls in order to parse a given string into its components. This is needed in Android since bionic does not support %as extension for sscanf.

---
 tools/perf/util/probe-event.c       |   25 ++++++++++++++++++-------
 tools/perf/util/trace-event-parse.c |   18 ++++++++----------
 2 files changed, 26 insertions(+), 17 deletions(-)
tools/perf/util/probe-event.c +1 -0
index 4ce04c2..685ddcf 100644
@@ -1100,6 +1100,7 @@ static int parse_probe_trace_command(const char *cmd,
 	struct probe_trace_point *tp = &tev->point;
 	char pr;
 	char *p;
+	char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
 	int ret, i, argc;
 	char **argv;
 

tools/perf/util/probe-event.c static int parse_probe_trace_command(const char *cmd, · line 1120

Quoted 8 lines
tools/perf/util/probe-event.c +1 -4
@@ -1116,14 +1117,19 @@ static int parse_probe_trace_command(const char *cmd,
 	}
 
 	/* Scan event and group name. */
-	ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
-		     &pr, (float *)(void *)&tev->group,
-		     (float *)(void *)&tev->event);
-	if (ret != 3) {
+	argv0_str = strdup(argv[0]);

It seems you need to check return value of strdup.

Quoted 16 lines
tools/perf/util/probe-event.c +8 -0
+	fmt1_str = strtok_r(argv0_str, ":", &fmt);
+	fmt2_str = strtok_r(NULL, "/", &fmt);
+	fmt3_str = strtok_r(NULL, " \t", &fmt);
+	if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
+	    || fmt3_str == NULL) {
 		semantic_error("Failed to parse event name: %s\n", argv[0]);
 		ret = -EINVAL;
 		goto out;
 	}
+	pr = fmt1_str[0];
+	tev->group = strdup(fmt2_str);
+	tev->event = strdup(fmt3_str);
 	pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
 
 	tp->retprobe = (pr == 'r');

tools/perf/util/probe-event.c static int parse_probe_trace_command(const char *cmd, · line 1145

Quoted 8 lines
tools/perf/util/probe-event.c +2 -3
@@ -1135,10 +1141,13 @@ static int parse_probe_trace_command(const char *cmd,
 		p++;
 	} else
 		p = argv[1];
-	ret = sscanf(p, "%a[^+]+%lu", (float *)(void *)&tp->symbol,
-		     &tp->offset);
-	if (ret == 1)
+	fmt1_str = strtok_r(p, "+", &fmt);
+	tp->symbol = strdup(fmt1_str);

Probably here too - although the original code didn't but I think it's needed.

Quoted 9 lines
tools/perf/util/probe-event.c +4 -0
+	fmt2_str = strtok_r(NULL, "", &fmt);
+	if (fmt2_str == NULL)
 		tp->offset = 0;
+	else
+		tp->offset = strtoul(fmt2_str, NULL, 10);
 
 	tev->nargs = argc - 2;
 	tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);

tools/perf/util/probe-event.c static int parse_probe_trace_command(const char *cmd, · line 1175

Quoted 5 lines
tools/perf/util/probe-event.c +2 -0
@@ -1162,6 +1171,8 @@ static int parse_probe_trace_command(const char *cmd,
 	}
 	ret = 0;
 out:
+	if (argv0_str)
+		free(argv0_str);

The free() can handle a NULL pointer safely.

Quoted 3 lines
tools/perf/util/probe-event.c +0 -0
 	argv_free(argv);
 	return ret;
 }
#3 lore raw

Re: [PATCH 1/4] perf tools: remove sscanf extension %as

Quoted 3 lines · Namhyung Kim

Hi Irina,

(Adding Masami to Cc)

Thanks Irina and Namhyung :)

Quoted 33 lines · Irina Tirdea
Quoted 32 lines

From: Irina Tirdea <[email protected]>

perf uses sscanf extension %as to read and allocate a string in the same step. This is a non-standard extension only present in new versions of glibc.

Replacing the use of sscanf and %as with strtok_r calls in order to parse a given string into its components. This is needed in Android since bionic does not support %as extension for sscanf.

---
 tools/perf/util/probe-event.c       |   25 ++++++++++++++++++-------
 tools/perf/util/trace-event-parse.c |   18 ++++++++----------
 2 files changed, 26 insertions(+), 17 deletions(-)
tools/perf/util/probe-event.c +1 -0
index 4ce04c2..685ddcf 100644
@@ -1100,6 +1100,7 @@ static int parse_probe_trace_command(const char *cmd,
 	struct probe_trace_point *tp = &tev->point;
 	char pr;
 	char *p;
+	char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
 	int ret, i, argc;
 	char **argv;
 
@@ -1116,14 +1117,19 @@ static int parse_probe_trace_command(const char *cmd,
 	}
 
Quoted 8 lines

tools/perf/util/probe-event.c static int parse_probe_trace_command(const char *cmd, · line 1120

Quoted 6 lines
tools/perf/util/probe-event.c +1 -4
 	/* Scan event and group name. */
-	ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
-		     &pr, (float *)(void *)&tev->group,
-		     (float *)(void *)&tev->event);
-	if (ret != 3) {
+	argv0_str = strdup(argv[0]);

It seems you need to check return value of strdup.

Agreed.

Quoted 14 lines
Quoted 14 lines
tools/perf/util/probe-event.c +8 -0
+	fmt1_str = strtok_r(argv0_str, ":", &fmt);
+	fmt2_str = strtok_r(NULL, "/", &fmt);
+	fmt3_str = strtok_r(NULL, " \t", &fmt);
+	if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
+	    || fmt3_str == NULL) {
 		semantic_error("Failed to parse event name: %s\n", argv[0]);
 		ret = -EINVAL;
 		goto out;
 	}
+	pr = fmt1_str[0];
+	tev->group = strdup(fmt2_str);
+	tev->event = strdup(fmt3_str);
 	pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
 
Quoted 13 lines

tools/perf/util/probe-event.c static int parse_probe_trace_command(const char *cmd, · line 1145

Quoted 10 lines
tools/perf/util/probe-event.c +2 -3
 	tp->retprobe = (pr == 'r');
@@ -1135,10 +1141,13 @@ static int parse_probe_trace_command(const char *cmd,
 		p++;
 	} else
 		p = argv[1];
-	ret = sscanf(p, "%a[^+]+%lu", (float *)(void *)&tp->symbol,
-		     &tp->offset);
-	if (ret == 1)
+	fmt1_str = strtok_r(p, "+", &fmt);
+	tp->symbol = strdup(fmt1_str);

Probably here too - although the original code didn't but I think it's needed.

right, it should be fixed.

Quoted 6 lines
Quoted 6 lines
tools/perf/util/probe-event.c +4 -0
+	fmt2_str = strtok_r(NULL, "", &fmt);
+	if (fmt2_str == NULL)
 		tp->offset = 0;
+	else
+		tp->offset = strtoul(fmt2_str, NULL, 10);
 
Quoted 10 lines

tools/perf/util/probe-event.c static int parse_probe_trace_command(const char *cmd, · line 1175

Quoted 8 lines
tools/perf/util/probe-event.c +2 -0
 	tev->nargs = argc - 2;
 	tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
@@ -1162,6 +1171,8 @@ static int parse_probe_trace_command(const char *cmd,
 	}
 	ret = 0;
 out:
+	if (argv0_str)
+		free(argv0_str);

The free() can handle a NULL pointer safely.

I don't care about it, since there are already lots of "if (ptr) free(ptr)" style code ;)

Quoted 4 lines

tools/perf/util/probe-event.c static int parse_probe_trace_command(const char *cmd, · line 1178

Quoted 4 lines
tools/perf/util/probe-event.c +0 -0
 	argv_free(argv);
 	return ret;
 }

Thank you!

Signature · 4 lines
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]
#4 lore raw

Re: [PATCH 1/4] perf tools: remove sscanf extension %as

Quoted 42 lines
Quoted 33 lines · Irina Tirdea
Quoted 32 lines

From: Irina Tirdea <[email protected]>

perf uses sscanf extension %as to read and allocate a string in the same step. This is a non-standard extension only present in new versions of glibc.

Replacing the use of sscanf and %as with strtok_r calls in order to parse a given string into its components. This is needed in Android since bionic does not support %as extension for sscanf.

---
 tools/perf/util/probe-event.c       |   25 ++++++++++++++++++-------
 tools/perf/util/trace-event-parse.c |   18 ++++++++----------
 2 files changed, 26 insertions(+), 17 deletions(-)
tools/perf/util/probe-event.c +1 -0
index 4ce04c2..685ddcf 100644
@@ -1100,6 +1100,7 @@ static int parse_probe_trace_command(const char *cmd,
     struct probe_trace_point *tp = &tev->point;
     char pr;
     char *p;
+    char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
     int ret, i, argc;
     char **argv;
 
@@ -1116,14 +1117,19 @@ static int parse_probe_trace_command(const char *cmd,
     }
 
Quoted 8 lines

tools/perf/util/probe-event.c static int parse_probe_trace_command(const char *cmd, · line 1120

Quoted 6 lines
tools/perf/util/probe-event.c +1 -4
     /* Scan event and group name. */
-    ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
-                 &pr, (float *)(void *)&tev->group,
-                 (float *)(void *)&tev->event);
-    if (ret != 3) {
+    argv0_str = strdup(argv[0]);

It seems you need to check return value of strdup.

Quoted 1 line

Agreed.

Thanks for the review! I'll make the fixes and resend the patch.

Irina

#5 lore raw

PATCH v2 1/1 perf tools: remove sscanf extension %as

In reply to Irina Tirdea

From: Irina Tirdea <[email protected]>

perf uses sscanf extension %as to read and allocate a string in the same step. This is a non-standard extension only present in new versions of glibc.

Replacing the use of sscanf and %as with strtok_r calls in order to parse a given string into its components. This is needed in Android since bionic does not support %as extension for sscanf.

---
 tools/perf/util/probe-event.c       |   36 ++++++++++++++++++++++++++++-------
 tools/perf/util/trace-event-parse.c |   18 ++++++++----------
 2 files changed, 37 insertions(+), 17 deletions(-)
tools/perf/util/probe-event.c +29 -7
index 4ce04c2..49a256e 100644
@@ -1100,6 +1100,7 @@ static int parse_probe_trace_command(const char *cmd,
 	struct probe_trace_point *tp = &tev->point;
 	char pr;
 	char *p;
+	char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
 	int ret, i, argc;
 	char **argv;
 
@@ -1116,14 +1117,27 @@ static int parse_probe_trace_command(const char *cmd,
 	}
 
 	/* Scan event and group name. */
-	ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
-		     &pr, (float *)(void *)&tev->group,
-		     (float *)(void *)&tev->event);
-	if (ret != 3) {
+	argv0_str = strdup(argv[0]);
+	if (argv0_str == NULL) {
+		ret = -ENOMEM;
+		goto out;
+	}
+	fmt1_str = strtok_r(argv0_str, ":", &fmt);
+	fmt2_str = strtok_r(NULL, "/", &fmt);
+	fmt3_str = strtok_r(NULL, " \t", &fmt);
+	if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
+	    || fmt3_str == NULL) {
 		semantic_error("Failed to parse event name: %s\n", argv[0]);
 		ret = -EINVAL;
 		goto out;
 	}
+	pr = fmt1_str[0];
+	tev->group = strdup(fmt2_str);
+	tev->event = strdup(fmt3_str);
+	if (tev->group == NULL || tev->event == NULL) {
+		ret = -ENOMEM;
+		goto out;
+	}
 	pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
 
 	tp->retprobe = (pr == 'r');
@@ -1135,10 +1149,17 @@ static int parse_probe_trace_command(const char *cmd,
 		p++;
 	} else
 		p = argv[1];
-	ret = sscanf(p, "%a[^+]+%lu", (float *)(void *)&tp->symbol,
-		     &tp->offset);
-	if (ret == 1)
+	fmt1_str = strtok_r(p, "+", &fmt);
+	tp->symbol = strdup(fmt1_str);
+	if (tp->symbol == NULL) {
+		ret = -ENOMEM;
+		goto out;
+	}
+	fmt2_str = strtok_r(NULL, "", &fmt);
+	if (fmt2_str == NULL)
 		tp->offset = 0;
+	else
+		tp->offset = strtoul(fmt2_str, NULL, 10);
 
 	tev->nargs = argc - 2;
 	tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
@@ -1162,6 +1183,7 @@ static int parse_probe_trace_command(const char *cmd,
 	}
 	ret = 0;
 out:
+	free(argv0_str);
 	argv_free(argv);
 	return ret;
 }
tools/perf/util/trace-event-parse.c +8 -10
index aa4c860..3aabcd6 100644
@@ -229,24 +229,22 @@ void parse_proc_kallsyms(struct pevent *pevent,
 	char *next = NULL;
 	char *addr_str;
 	char *mod;
-	char ch;
+	char *fmt;
 
 	line = strtok_r(file, "\n", &next);
 	while (line) {
 		mod = NULL;
-		sscanf(line, "%as %c %as\t[%as",
-		       (float *)(void *)&addr_str, /* workaround gcc warning */
-		       &ch, (float *)(void *)&func, (float *)(void *)&mod);
+		addr_str = strtok_r(line, " ", &fmt);
 		addr = strtoull(addr_str, NULL, 16);
-		free(addr_str);
-
-		/* truncate the extra ']' */
+		/* skip character */
+		strtok_r(NULL, " ", &fmt);
+		func = strtok_r(NULL, "\t", &fmt);
+		mod = strtok_r(NULL, "]", &fmt);
+		/* truncate the extra '[' */
 		if (mod)
-			mod[strlen(mod) - 1] = 0;
+			mod = mod + 1;
 
 		pevent_register_function(pevent, func, addr, mod);
-		free(func);
-		free(mod);
 
 		line = strtok_r(NULL, "\n", &next);
 	}
Signature · 1 line
1.7.9.5
#6 lore raw

Re: [PATCH v2 1/1] perf tools: remove sscanf extension %as

Em Thu, Sep 20, 2012 at 11:37:50PM +0300, [email protected] escreveu:

Quoted 10 lines

From: Irina Tirdea <[email protected]>

perf uses sscanf extension %as to read and allocate a string in the same step. This is a non-standard extension only present in new versions of glibc.

Replacing the use of sscanf and %as with strtok_r calls in order to parse a given string into its components. This is needed in Android since bionic does not support %as extension for sscanf.

Masami, can I have your Acked-by or even a Tested-by?

- Arnaldo

Quoted 118 lines
---
 tools/perf/util/probe-event.c       |   36 ++++++++++++++++++++++++++++-------
 tools/perf/util/trace-event-parse.c |   18 ++++++++----------
 2 files changed, 37 insertions(+), 17 deletions(-)
tools/perf/util/probe-event.c +29 -7
index 4ce04c2..49a256e 100644
@@ -1100,6 +1100,7 @@ static int parse_probe_trace_command(const char *cmd,
 	struct probe_trace_point *tp = &tev->point;
 	char pr;
 	char *p;
+	char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
 	int ret, i, argc;
 	char **argv;
 
@@ -1116,14 +1117,27 @@ static int parse_probe_trace_command(const char *cmd,
 	}
 
 	/* Scan event and group name. */
-	ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
-		     &pr, (float *)(void *)&tev->group,
-		     (float *)(void *)&tev->event);
-	if (ret != 3) {
+	argv0_str = strdup(argv[0]);
+	if (argv0_str == NULL) {
+		ret = -ENOMEM;
+		goto out;
+	}
+	fmt1_str = strtok_r(argv0_str, ":", &fmt);
+	fmt2_str = strtok_r(NULL, "/", &fmt);
+	fmt3_str = strtok_r(NULL, " \t", &fmt);
+	if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
+	    || fmt3_str == NULL) {
 		semantic_error("Failed to parse event name: %s\n", argv[0]);
 		ret = -EINVAL;
 		goto out;
 	}
+	pr = fmt1_str[0];
+	tev->group = strdup(fmt2_str);
+	tev->event = strdup(fmt3_str);
+	if (tev->group == NULL || tev->event == NULL) {
+		ret = -ENOMEM;
+		goto out;
+	}
 	pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
 
 	tp->retprobe = (pr == 'r');
@@ -1135,10 +1149,17 @@ static int parse_probe_trace_command(const char *cmd,
 		p++;
 	} else
 		p = argv[1];
-	ret = sscanf(p, "%a[^+]+%lu", (float *)(void *)&tp->symbol,
-		     &tp->offset);
-	if (ret == 1)
+	fmt1_str = strtok_r(p, "+", &fmt);
+	tp->symbol = strdup(fmt1_str);
+	if (tp->symbol == NULL) {
+		ret = -ENOMEM;
+		goto out;
+	}
+	fmt2_str = strtok_r(NULL, "", &fmt);
+	if (fmt2_str == NULL)
 		tp->offset = 0;
+	else
+		tp->offset = strtoul(fmt2_str, NULL, 10);
 
 	tev->nargs = argc - 2;
 	tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
@@ -1162,6 +1183,7 @@ static int parse_probe_trace_command(const char *cmd,
 	}
 	ret = 0;
 out:
+	free(argv0_str);
 	argv_free(argv);
 	return ret;
 }
tools/perf/util/trace-event-parse.c +8 -10
index aa4c860..3aabcd6 100644
@@ -229,24 +229,22 @@ void parse_proc_kallsyms(struct pevent *pevent,
 	char *next = NULL;
 	char *addr_str;
 	char *mod;
-	char ch;
+	char *fmt;
 
 	line = strtok_r(file, "\n", &next);
 	while (line) {
 		mod = NULL;
-		sscanf(line, "%as %c %as\t[%as",
-		       (float *)(void *)&addr_str, /* workaround gcc warning */
-		       &ch, (float *)(void *)&func, (float *)(void *)&mod);
+		addr_str = strtok_r(line, " ", &fmt);
 		addr = strtoull(addr_str, NULL, 16);
-		free(addr_str);
-
-		/* truncate the extra ']' */
+		/* skip character */
+		strtok_r(NULL, " ", &fmt);
+		func = strtok_r(NULL, "\t", &fmt);
+		mod = strtok_r(NULL, "]", &fmt);
+		/* truncate the extra '[' */
 		if (mod)
-			mod[strlen(mod) - 1] = 0;
+			mod = mod + 1;
 
 		pevent_register_function(pevent, func, addr, mod);
-		free(func);
-		free(mod);
 
 		line = strtok_r(NULL, "\n", &next);
 	}
Signature · 1 line
1.7.9.5
Acked-by on v2 1/1
#7 lore raw

Re: [PATCH v2 1/1] perf tools: remove sscanf extension %as

In reply to [email protected]
Quoted 10 lines · (2012/09/21 5:37), [email protected] wrote:

From: Irina Tirdea <[email protected]>

perf uses sscanf extension %as to read and allocate a string in the same step. This is a non-standard extension only present in new versions of glibc.

Replacing the use of sscanf and %as with strtok_r calls in order to parse a given string into its components. This is needed in Android since bionic does not support %as extension for sscanf.

OK, I've reviewed and tested, looks good! :)

Thank you!

Quoted 118 lines
---
 tools/perf/util/probe-event.c       |   36 ++++++++++++++++++++++++++++-------
 tools/perf/util/trace-event-parse.c |   18 ++++++++----------
 2 files changed, 37 insertions(+), 17 deletions(-)
tools/perf/util/probe-event.c +29 -7
index 4ce04c2..49a256e 100644
@@ -1100,6 +1100,7 @@ static int parse_probe_trace_command(const char *cmd,
 	struct probe_trace_point *tp = &tev->point;
 	char pr;
 	char *p;
+	char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
 	int ret, i, argc;
 	char **argv;
 
@@ -1116,14 +1117,27 @@ static int parse_probe_trace_command(const char *cmd,
 	}
 
 	/* Scan event and group name. */
-	ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
-		     &pr, (float *)(void *)&tev->group,
-		     (float *)(void *)&tev->event);
-	if (ret != 3) {
+	argv0_str = strdup(argv[0]);
+	if (argv0_str == NULL) {
+		ret = -ENOMEM;
+		goto out;
+	}
+	fmt1_str = strtok_r(argv0_str, ":", &fmt);
+	fmt2_str = strtok_r(NULL, "/", &fmt);
+	fmt3_str = strtok_r(NULL, " \t", &fmt);
+	if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
+	    || fmt3_str == NULL) {
 		semantic_error("Failed to parse event name: %s\n", argv[0]);
 		ret = -EINVAL;
 		goto out;
 	}
+	pr = fmt1_str[0];
+	tev->group = strdup(fmt2_str);
+	tev->event = strdup(fmt3_str);
+	if (tev->group == NULL || tev->event == NULL) {
+		ret = -ENOMEM;
+		goto out;
+	}
 	pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
 
 	tp->retprobe = (pr == 'r');
@@ -1135,10 +1149,17 @@ static int parse_probe_trace_command(const char *cmd,
 		p++;
 	} else
 		p = argv[1];
-	ret = sscanf(p, "%a[^+]+%lu", (float *)(void *)&tp->symbol,
-		     &tp->offset);
-	if (ret == 1)
+	fmt1_str = strtok_r(p, "+", &fmt);
+	tp->symbol = strdup(fmt1_str);
+	if (tp->symbol == NULL) {
+		ret = -ENOMEM;
+		goto out;
+	}
+	fmt2_str = strtok_r(NULL, "", &fmt);
+	if (fmt2_str == NULL)
 		tp->offset = 0;
+	else
+		tp->offset = strtoul(fmt2_str, NULL, 10);
 
 	tev->nargs = argc - 2;
 	tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
@@ -1162,6 +1183,7 @@ static int parse_probe_trace_command(const char *cmd,
 	}
 	ret = 0;
 out:
+	free(argv0_str);
 	argv_free(argv);
 	return ret;
 }
tools/perf/util/trace-event-parse.c +8 -10
index aa4c860..3aabcd6 100644
@@ -229,24 +229,22 @@ void parse_proc_kallsyms(struct pevent *pevent,
 	char *next = NULL;
 	char *addr_str;
 	char *mod;
-	char ch;
+	char *fmt;
 
 	line = strtok_r(file, "\n", &next);
 	while (line) {
 		mod = NULL;
-		sscanf(line, "%as %c %as\t[%as",
-		       (float *)(void *)&addr_str, /* workaround gcc warning */
-		       &ch, (float *)(void *)&func, (float *)(void *)&mod);
+		addr_str = strtok_r(line, " ", &fmt);
 		addr = strtoull(addr_str, NULL, 16);
-		free(addr_str);
-
-		/* truncate the extra ']' */
+		/* skip character */
+		strtok_r(NULL, " ", &fmt);
+		func = strtok_r(NULL, "\t", &fmt);
+		mod = strtok_r(NULL, "]", &fmt);
+		/* truncate the extra '[' */
 		if (mod)
-			mod[strlen(mod) - 1] = 0;
+			mod = mod + 1;
 
 		pevent_register_function(pevent, func, addr, mod);
-		free(func);
-		free(mod);
 
 		line = strtok_r(NULL, "\n", &next);
 	}
Signature · 4 lines
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]
#8 lore raw

[tip:perf/core] perf tools: remove sscanf extension %as

In reply to [email protected]
Commit-ID:  bcbd004020bf0d725722be75da35fd326ff63ef4
Gitweb:     http://git.kernel.org/tip/bcbd004020bf0d725722be75da35fd326ff63ef4
Author:     Irina Tirdea <[email protected]>
AuthorDate: Thu, 20 Sep 2012 23:37:50 +0300
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Mon, 24 Sep 2012 11:49:31 -0300

perf tools: remove sscanf extension %as

perf uses sscanf extension %as to read and allocate a string in the same step. This is a non-standard extension only present in new versions of glibc.

Replacing the use of sscanf and %as with strtok_r calls in order to parse a given string into its components. This is needed in Android since bionic does not support
%as extension for sscanf.

---
 tools/perf/util/probe-event.c       |   36 ++++++++++++++++++++++++++++------
 tools/perf/util/trace-event-parse.c |   18 +++++++---------
 2 files changed, 37 insertions(+), 17 deletions(-)
tools/perf/util/probe-event.c +29 -7
index 4ce04c2..49a256e 100644
@@ -1100,6 +1100,7 @@ static int parse_probe_trace_command(const char *cmd,
 	struct probe_trace_point *tp = &tev->point;
 	char pr;
 	char *p;
+	char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
 	int ret, i, argc;
 	char **argv;
 
@@ -1116,14 +1117,27 @@ static int parse_probe_trace_command(const char *cmd,
 	}
 
 	/* Scan event and group name. */
-	ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
-		     &pr, (float *)(void *)&tev->group,
-		     (float *)(void *)&tev->event);
-	if (ret != 3) {
+	argv0_str = strdup(argv[0]);
+	if (argv0_str == NULL) {
+		ret = -ENOMEM;
+		goto out;
+	}
+	fmt1_str = strtok_r(argv0_str, ":", &fmt);
+	fmt2_str = strtok_r(NULL, "/", &fmt);
+	fmt3_str = strtok_r(NULL, " \t", &fmt);
+	if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
+	    || fmt3_str == NULL) {
 		semantic_error("Failed to parse event name: %s\n", argv[0]);
 		ret = -EINVAL;
 		goto out;
 	}
+	pr = fmt1_str[0];
+	tev->group = strdup(fmt2_str);
+	tev->event = strdup(fmt3_str);
+	if (tev->group == NULL || tev->event == NULL) {
+		ret = -ENOMEM;
+		goto out;
+	}
 	pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
 
 	tp->retprobe = (pr == 'r');
@@ -1135,10 +1149,17 @@ static int parse_probe_trace_command(const char *cmd,
 		p++;
 	} else
 		p = argv[1];
-	ret = sscanf(p, "%a[^+]+%lu", (float *)(void *)&tp->symbol,
-		     &tp->offset);
-	if (ret == 1)
+	fmt1_str = strtok_r(p, "+", &fmt);
+	tp->symbol = strdup(fmt1_str);
+	if (tp->symbol == NULL) {
+		ret = -ENOMEM;
+		goto out;
+	}
+	fmt2_str = strtok_r(NULL, "", &fmt);
+	if (fmt2_str == NULL)
 		tp->offset = 0;
+	else
+		tp->offset = strtoul(fmt2_str, NULL, 10);
 
 	tev->nargs = argc - 2;
 	tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
@@ -1162,6 +1183,7 @@ static int parse_probe_trace_command(const char *cmd,
 	}
 	ret = 0;
 out:
+	free(argv0_str);
 	argv_free(argv);
 	return ret;
 }
tools/perf/util/trace-event-parse.c +8 -10
index aa4c860..3aabcd6 100644
@@ -229,24 +229,22 @@ void parse_proc_kallsyms(struct pevent *pevent,
 	char *next = NULL;
 	char *addr_str;
 	char *mod;
-	char ch;
+	char *fmt;
 
 	line = strtok_r(file, "\n", &next);
 	while (line) {
 		mod = NULL;
-		sscanf(line, "%as %c %as\t[%as",
-		       (float *)(void *)&addr_str, /* workaround gcc warning */
-		       &ch, (float *)(void *)&func, (float *)(void *)&mod);
+		addr_str = strtok_r(line, " ", &fmt);
 		addr = strtoull(addr_str, NULL, 16);
-		free(addr_str);
-
-		/* truncate the extra ']' */
+		/* skip character */
+		strtok_r(NULL, " ", &fmt);
+		func = strtok_r(NULL, "\t", &fmt);
+		mod = strtok_r(NULL, "]", &fmt);
+		/* truncate the extra '[' */
 		if (mod)
-			mod[strlen(mod) - 1] = 0;
+			mod = mod + 1;
 
 		pevent_register_function(pevent, func, addr, mod);
-		free(func);
-		free(mod);
 
 		line = strtok_r(NULL, "\n", &next);
 	}
#9 lore raw

PATCH 2/4 perf stat: add compile-time option to disable --big-num

In reply to Irina Tirdea

From: Irina Tirdea <[email protected]>

In printf's format, ' is used to group the output with thousands' grouping characters for decimal conversion. Bionic does not support ' for printf.

Add a compile-time option (NO_BIG_NUM) to disable the --big-num option from perf stat.

---
 tools/perf/Makefile                 |    8 ++++++++
 tools/perf/builtin-stat.c           |    8 ++++++++
 tools/perf/config/feature-tests.mak |   12 ++++++++++++
 3 files changed, 28 insertions(+)
tools/perf/Makefile +8 -0
index 209774b..8daa781 100644
@@ -768,6 +768,14 @@ else
        endif
 endif
 
+ifdef NO_BIG_NUM
+	BASIC_CFLAGS += -DNO_BIG_NUM
+else
+	ifneq ($(call try-cc,$(SOURCE_BIG_NUM),),y)
+		BASIC_CFLAGS += -DNO_BIG_NUM
+	endif
+endif
+
 ifdef ASCIIDOC8
 	export ASCIIDOC8
 endif
tools/perf/builtin-stat.c +8 -0
index dab347d..ad8013e 100644
@@ -188,7 +188,11 @@ static pid_t			child_pid			= -1;
 static bool			null_run			=  false;
 static int			detailed_run			=  0;
 static bool			sync_run			=  false;
+#ifdef NO_BIG_NUM
+static bool			big_num				=  false;
+#else
 static bool			big_num				=  true;
+#endif
 static int			big_num_opt			=  -1;
 static const char		*csv_sep			= NULL;
 static bool			csv_output			= false;
@@ -1075,12 +1079,14 @@ static const char * const stat_usage[] = {
 	NULL
 };
 
+#ifndef NO_BIG_NUM
 static int stat__set_big_num(const struct option *opt __maybe_unused,
 			     const char *s __maybe_unused, int unset)
 {
 	big_num_opt = unset ? 0 : 1;
 	return 0;
 }
+#endif
 
 static bool append_file;
 
@@ -1112,9 +1118,11 @@ static const struct option options[] = {
 		    "detailed run - start a lot of events"),
 	OPT_BOOLEAN('S', "sync", &sync_run,
 		    "call sync() before starting a run"),
+#ifndef NO_BIG_NUM
 	OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL, 
 			   "print large numbers with thousands\' separators",
 			   stat__set_big_num),
+#endif
 	OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
 		    "list of cpus to monitor in system-wide"),
 	OPT_BOOLEAN('A', "no-aggr", &no_aggr,
tools/perf/config/feature-tests.mak +12 -0
index 116690a..2e5fad7 100644
@@ -193,3 +193,15 @@ int main(void)
 }
 endef
 endif
+
+ifndef NO_BIG_NUM
+define SOURCE_BIG_NUM
+#include <stdio.h>
+
+int main(void)
+{
+	printf(\"%'\''.2f\", 1234567.89);
+	return 0;
+}
+endef
+endif
Signature · 1 line
1.7.9.5
#10 lore raw

Re: [PATCH 2/4] perf stat: add compile-time option to disable --big-num

Quoted 4 lines · Irina Tirdea

From: Irina Tirdea <[email protected]>

In printf's format, ' is used to group the output with thousands' grouping characters for decimal conversion. Bionic does not support ' for printf.

Please try to solve compatibility without affecting the default output and big num is the default perf stat output.

Could the big num output be implemented within perf, without relying on glibc's printf implementation?

Thanks,

Ingo

#11 lore raw

Re: [PATCH 2/4] perf stat: add compile-time option to disable --big-num

Quoted 10 lines · Ingo Molnar
Quoted 4 lines · Irina Tirdea

From: Irina Tirdea <[email protected]>

In printf's format, ' is used to group the output with thousands' grouping characters for decimal conversion. Bionic does not support ' for printf.

Please try to solve compatibility without affecting the default output and big num is the default perf stat output.

Understood.

Quoted 3 lines

Could the big num output be implemented within perf, without relying on glibc's printf implementation?

Yes. I'll write a function that does the grouping instead of using printf and send it for review instead of this patch.

Thanks,
Irina

#12 lore raw

PATCH v2 1/1 perf stat: implement --big-num grouping

In reply to Irina Tirdea

From: Irina Tirdea <[email protected]>

In glibc, printf supports ' to group numbers with thousands' grouping characters. Bionic does not support ' for printf.

Implement thousands's grouping for numbers according to locale. The implementation uses the algorithm from glibc
(http://www.gnu.org/software/libc/).

Bionic does not implement locales, so we need to add a configuration option NO_LOCALE. If NO_LOCALE is defined, default values for thousands separator and grouping are used.

---

Changes in v2:
This is a rewrite of http://lkml.org/lkml/2012/9/13/574
Instead of disabling big num for Android, I added the implementation in perf (as Ingo suggested).

 tools/perf/Makefile                 |    8 +++
 tools/perf/builtin-stat.c           |  112 ++++++++++++++++++++++++++++++++---
 tools/perf/config/feature-tests.mak |   18 ++++++
 3 files changed, 131 insertions(+), 7 deletions(-)
tools/perf/Makefile +8 -0
index 5077f8e..74e21cf 100644
@@ -769,6 +769,14 @@ else
        endif
 endif
 
+ifdef NO_LOCALE
+	BASIC_CFLAGS += -DNO_LOCALE
+else
+	ifneq ($(call try-cc,$(SOURCE_LOCALE),),y)
+		BASIC_CFLAGS += -DNO_LOCALE
+	endif
+endif
+
 ifdef ASCIIDOC8
 	export ASCIIDOC8
 endif
tools/perf/builtin-stat.c +105 -7

136 lines not on this page · raw message

tools/perf/config/feature-tests.mak +18 -0
index 116690a..7e5c005 100644
@@ -193,3 +193,21 @@ int main(void)
 }
 endef
 endif
+
+ifndef NO_LOCALE
+define SOURCE_LOCALE
+#include <locale.h>
+
+int main(void)
+{
+	char *thousands_sep, *grouping;
+
+	struct lconv *lc = localeconv();
+	if (lc != NULL) {
+		thousands_sep = lc->thousands_sep;
+		grouping = lc->grouping;
+	}
+	return 0;
+}
+endef
+endif
Signature · 1 line
1.7.9.5
#13 lore raw

PATCH 3/4 perf archive: remove -f from the rm command

In reply to Irina Tirdea

From: Irina Tirdea <[email protected]>

In Android, rm does not support the -f parameter. Remove -f from rm and make sure rm does not fail even if the files to be removed are not found.

---
 tools/perf/perf-archive.sh |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/perf/perf-archive.sh +2 -2
index 95b6f8b..da94179 100644
@@ -24,7 +24,7 @@ NOBUILDID=0000000000000000000000000000000000000000
 perf buildid-list -i $PERF_DATA --with-hits | grep -v "^$NOBUILDID " > $BUILDIDS
 if [ ! -s $BUILDIDS ] ; then
 	echo "perf archive: no build-ids found"
-	rm -f $BUILDIDS
+	rm $BUILDIDS || true
 	exit 1
 fi
 
@@ -40,7 +40,7 @@ while read build_id ; do
 done
 
 tar cfj $PERF_DATA.tar.bz2 -C $PERF_BUILDID_DIR -T $MANIFEST
-rm -f $MANIFEST $BUILDIDS
+rm $MANIFEST $BUILDIDS || true
 echo -e "Now please run:\n"
 echo -e "$ tar xvf $PERF_DATA.tar.bz2 -C ~/.debug\n"
 echo "wherever you need to run 'perf report' on."
Signature · 1 line
1.7.9.5
#14 lore raw

[tip:perf/core] perf archive: Remove -f from the rm command

Commit-ID:  73eb422c10aa2a4afc1100d1bd54974ed72ad373
Gitweb:     http://git.kernel.org/tip/73eb422c10aa2a4afc1100d1bd54974ed72ad373
Author:     Irina Tirdea <[email protected]>
AuthorDate: Fri, 14 Sep 2012 01:07:42 +0300
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Mon, 17 Sep 2012 13:10:27 -0300

perf archive: Remove -f from the rm command

In Android, rm does not support the -f parameter.

Remove -f from rm and make sure rm does not fail even if the files to be removed are not found.

---
 tools/perf/perf-archive.sh |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
tools/perf/perf-archive.sh +2 -2
index 95b6f8b..da94179 100644
@@ -24,7 +24,7 @@ NOBUILDID=0000000000000000000000000000000000000000
 perf buildid-list -i $PERF_DATA --with-hits | grep -v "^$NOBUILDID " > $BUILDIDS
 if [ ! -s $BUILDIDS ] ; then
 	echo "perf archive: no build-ids found"
-	rm -f $BUILDIDS
+	rm $BUILDIDS || true
 	exit 1
 fi
 
@@ -40,7 +40,7 @@ while read build_id ; do
 done
 
 tar cfj $PERF_DATA.tar.bz2 -C $PERF_BUILDID_DIR -T $MANIFEST
-rm -f $MANIFEST $BUILDIDS
+rm $MANIFEST $BUILDIDS || true
 echo -e "Now please run:\n"
 echo -e "$ tar xvf $PERF_DATA.tar.bz2 -C ~/.debug\n"
 echo "wherever you need to run 'perf report' on."
#15 lore raw

PATCH 4/4 perf archive: make f the last parameter for tar

In reply to Irina Tirdea

From: Irina Tirdea <[email protected]>

On some systems, tar needs to specify the name of the archive immediately after the -f parameter.

Change the order of the parameters so tar can run properly.

---
 tools/perf/perf-archive.sh |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/perf-archive.sh +1 -1
index da94179..e919306 100644
@@ -39,7 +39,7 @@ while read build_id ; do
 	echo ${filename#$PERF_BUILDID_LINKDIR} >> $MANIFEST
 done
 
-tar cfj $PERF_DATA.tar.bz2 -C $PERF_BUILDID_DIR -T $MANIFEST
+tar cjf $PERF_DATA.tar.bz2 -C $PERF_BUILDID_DIR -T $MANIFEST
 rm $MANIFEST $BUILDIDS || true
 echo -e "Now please run:\n"
 echo -e "$ tar xvf $PERF_DATA.tar.bz2 -C ~/.debug\n"
Signature · 1 line
1.7.9.5
#16 lore raw

[tip:perf/core] perf archive: Make 'f' the last parameter for tar

Commit-ID:  87ff50a3192152944d8e65b485bbf3d03214d0b6
Gitweb:     http://git.kernel.org/tip/87ff50a3192152944d8e65b485bbf3d03214d0b6
Author:     Irina Tirdea <[email protected]>
AuthorDate: Fri, 14 Sep 2012 01:07:43 +0300
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Mon, 17 Sep 2012 13:10:42 -0300

perf archive: Make 'f' the last parameter for tar

On some systems, tar needs to specify the name of the archive immediately after the -f parameter.

Change the order of the parameters so tar can run properly.

---
 tools/perf/perf-archive.sh |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
tools/perf/perf-archive.sh +1 -1
index da94179..e919306 100644
@@ -39,7 +39,7 @@ while read build_id ; do
 	echo ${filename#$PERF_BUILDID_LINKDIR} >> $MANIFEST
 done
 
-tar cfj $PERF_DATA.tar.bz2 -C $PERF_BUILDID_DIR -T $MANIFEST
+tar cjf $PERF_DATA.tar.bz2 -C $PERF_BUILDID_DIR -T $MANIFEST
 rm $MANIFEST $BUILDIDS || true
 echo -e "Now please run:\n"
 echo -e "$ tar xvf $PERF_DATA.tar.bz2 -C ~/.debug\n"