Skip to contentSkip to conversation

perf tools: fix bug in usage of the basename() function

PATCH Superseded Stephane Eranian 2 participants 4 messages Dec 5, 2013 – Dec 10, 2013 perf events

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf tools: fix bug in usage of the basename() function

The basename() implementation varies a lot between systems. The Linux man page says: "basename may modify the content of the path, so it may be desirable to pass a copy when calling the function". On some other systems, the returned address may come from an internal buffer which can be reused in subsequent calls, thus the results should also be copied.

The dso__set_basename() function was not doing this causing problems on some systems with wrong library names being shown by perf report, such as on Android systems.

This patch fixes the problem.
Thanks to Ben Cheng for tracking down the problem.

Patch relative to tip.git at commit 631d5ea.

---
 tools/perf/util/dso.c |   29 ++++++++++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)
tools/perf/util/dso.c +28 -1
index af4c687c..d186ace 100644
@@ -404,7 +404,34 @@ void dso__set_short_name(struct dso *dso, const char *name)
 
 static void dso__set_basename(struct dso *dso)
 {
-	dso__set_short_name(dso, basename(dso->long_name));
+	char *lname, *base;
+
+	/*
+	 * basename may modify path buffer, so we must pass
+	 * a copy.
+	 */
+	lname = strdup(dso->long_name);
+	if (!lname)
+		return;
+
+	/*
+	 * basename may return pointer to internal
+	 * storage which is reused in subsequent calls
+	 * so copy the result
+	 */
+	base = strdup(basename(lname));
+
+	free(lname);
+
+	if (!base)
+		return;
+
+	if (dso->sname_alloc)
+		free((char *)dso->short_name);
+	else
+		dso->sname_alloc = 1;
+
+	dso__set_short_name(dso, base);
 }
 
 int dso__name_len(const struct dso *dso)
Signature · 1 line
1.7.9.5
Acked-by on v1 1/1
#1 lore raw
Quoted 12 lines · Stephane Eranian

The basename() implementation varies a lot between systems. The Linux man page says: "basename may modify the content of the path, so it may be desirable to pass a copy when calling the function". On some other systems, the returned address may come from an internal buffer which can be reused in subsequent calls, thus the results should also be copied.

The dso__set_basename() function was not doing this causing problems on some systems with wrong library names being shown by perf report, such as on Android systems.

Quoted 8 lines

This patch fixes the problem.
Thanks to Ben Cheng for tracking down the problem.

Patch relative to tip.git at commit 631d5ea.

---

Just three nits:

Quoted 8 lines
 tools/perf/util/dso.c |   29 ++++++++++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)
tools/perf/util/dso.c +0 -0
index af4c687c..d186ace 100644

tools/perf/util/dso.c void dso__set_short_name(struct dso *dso, const char *name) · line 411

Quoted 9 lines
tools/perf/util/dso.c +5 -1
@@ -404,7 +404,34 @@ void dso__set_short_name(struct dso *dso, const char *name)
 
 static void dso__set_basename(struct dso *dso)
 {
-	dso__set_short_name(dso, basename(dso->long_name));
+	char *lname, *base;
+
+	/*
+	 * basename may modify path buffer, so we must pass
+	 * a copy.

s/basename may modify path buffer
/basename() may modify the path buffer

tools/perf/util/dso.c void dso__set_short_name(struct dso *dso, const char *name) · line 421

Quoted 10 lines
tools/perf/util/dso.c +10 -0
+	 */
+	lname = strdup(dso->long_name);
+	if (!lname)
+		return;
+
+	/*
+	 * basename may return pointer to internal
+	 * storage which is reused in subsequent calls
+	 * so copy the result
+	 */

s/basename may return pointer
/basename() may return a pointer

Makes for easier reading.

(Also please use consistent periods - the first comment has a period, the second one doesn't. ':' works well too:)

tools/perf/util/dso.c void dso__set_short_name(struct dso *dso, const char *name) · line 430

Quoted 9 lines
tools/perf/util/dso.c +9 -0
+	base = strdup(basename(lname));
+
+	free(lname);
+
+	if (!base)
+		return;
+
+	if (dso->sname_alloc)
+		free((char *)dso->short_name);

That cast is probably not needed.

tools/perf/util/dso.c void dso__set_short_name(struct dso *dso, const char *name) · line 437

Quoted 7 lines
tools/perf/util/dso.c +4 -0
+	else
+		dso->sname_alloc = 1;
+
+	dso__set_short_name(dso, base);
 }
 
 int dso__name_len(const struct dso *dso)

Otherwise:

Thanks,

Ingo

#2 lore raw
Quoted 58 lines · Ingo Molnar
Quoted 12 lines · Stephane Eranian

The basename() implementation varies a lot between systems. The Linux man page says: "basename may modify the content of the path, so it may be desirable to pass a copy when calling the function". On some other systems, the returned address may come from an internal buffer which can be reused in subsequent calls, thus the results should also be copied.

The dso__set_basename() function was not doing this causing problems on some systems with wrong library names being shown by perf report, such as on Android systems.

Quoted 8 lines

This patch fixes the problem.
Thanks to Ben Cheng for tracking down the problem.

Patch relative to tip.git at commit 631d5ea.

---

Just three nits:

Quoted 8 lines
 tools/perf/util/dso.c |   29 ++++++++++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)
tools/perf/util/dso.c +0 -0
index af4c687c..d186ace 100644

tools/perf/util/dso.c void dso__set_short_name(struct dso *dso, const char *name) · line 411

Quoted 9 lines
tools/perf/util/dso.c +5 -1
@@ -404,7 +404,34 @@ void dso__set_short_name(struct dso *dso, const char *name)
 
 static void dso__set_basename(struct dso *dso)
 {
-     dso__set_short_name(dso, basename(dso->long_name));
+     char *lname, *base;
+
+     /*
+      * basename may modify path buffer, so we must pass
+      * a copy.

s/basename may modify path buffer
/basename() may modify the path buffer

tools/perf/util/dso.c void dso__set_short_name(struct dso *dso, const char *name) · line 421

Quoted 10 lines
tools/perf/util/dso.c +10 -0
+      */
+     lname = strdup(dso->long_name);
+     if (!lname)
+             return;
+
+     /*
+      * basename may return pointer to internal
+      * storage which is reused in subsequent calls
+      * so copy the result
+      */
Quoted 8 lines

s/basename may return pointer
/basename() may return a pointer

Makes for easier reading.

(Also please use consistent periods - the first comment has a period, the second one doesn't. ':' works well too:)

Fixed that.

Quoted 13 lines

tools/perf/util/dso.c void dso__set_short_name(struct dso *dso, const char *name) · line 430

Quoted 10 lines
tools/perf/util/dso.c +9 -0
+     base = strdup(basename(lname));
+
+     free(lname);
+
+     if (!base)
+             return;
+
+     if (dso->sname_alloc)
+             free((char *)dso->short_name);

That cast is probably not needed.

It is with my compiler. It prints out a warning otherwise.

Quoted 8 lines

tools/perf/util/dso.c void dso__set_short_name(struct dso *dso, const char *name) · line 437

Quoted 7 lines
tools/perf/util/dso.c +4 -0
+     else
+             dso->sname_alloc = 1;
+
+     dso__set_short_name(dso, base);
 }
 
 int dso__name_len(const struct dso *dso)
Quoted 7 lines

Otherwise:

Thanks,

Ingo

Reposting ASAP.
Thanks.

#3 lore raw
Quoted 12 lines · Stephane Eranian
Quoted 12 lines

tools/perf/util/dso.c void dso__set_short_name(struct dso *dso, const char *name) · line 430

Quoted 9 lines
tools/perf/util/dso.c +9 -0
+     base = strdup(basename(lname));
+
+     free(lname);
+
+     if (!base)
+             return;
+
+     if (dso->sname_alloc)
+             free((char *)dso->short_name);

That cast is probably not needed.

Quoted 1 line

It is with my compiler. It prints out a warning otherwise.

Yeah, see my previous mail, I think having dso->short_name as 'const' is a mistake, as there are really just two main usecases for methods that operate on 'struct dso':

- life time affecting (setup/free) methods which need access to all fields, which don't want dso->short_name as a const (as evidenced by the cast).

- actual usage methods that get a 'const struct dso' anyway, so they don't need dso->short_name as a const.

Thanks,

Ingo