Skip to contentSkip to conversation

perf-probe bugfixes

PATCH In review Masami Hiramatsu 3 participants 12 messages Sep 30, 2015 – Oct 1, 2015 perf events

lore
Share & tools
lore
#0 lore raw

cover perf-probe bugfixes

Hi,

Here is a bunch of bugfix patches for perf-probe.

- Begin and end report session correctly, which fixes a mistake of libdwfl usage.
- Fix to remove dot suffix from second or latter events which fixes a bug on multiple events on a dot suffixed symbol.
- Show correct source lines of probes on kmodules, which fixes a bug that perf-probe --list can not find probe points in kernel modules.
- Fix a segfault bug in debuginfo_cache, which fixes a segfault bug in debuginfo_cache__open.
- Improve error message when %return is on inlined function is not actual bugfix, but improve an error message.

Thank you,

---

Masami Hiramatsu (5):
[BUGFIX] perf-probe: Begin and end report session correctly [BUGFIX] perf-probe: Fix to remove dot suffix from second or latter events [BUGFIX] perf-probe: Show correct source lines of probes on kmodules [BUGFIX] perf-probe: Fix a segfault bug in debuginfo_cache
perf-probe: Improve error message when %return is on inlined function

 tools/perf/util/probe-event.c  |   89 +++++++++++++++++++++++++---------------
 tools/perf/util/probe-finder.c |   58 ++++++++++++++++++++++++--
 2 files changed, 109 insertions(+), 38 deletions(-)
#1 lore raw

PATCH 1/5 perf-probe: Begin and end report session correctly

Fix a trival bug about libdwfl usage of the report session, it should be explicitly do begin and end report session around report_offline.

---
 tools/perf/util/probe-finder.c |    3 +++
 1 file changed, 3 insertions(+)
tools/perf/util/probe-finder.c +3 -0
index 29c43c068..35f905f 100644
@@ -70,6 +70,7 @@ static int debuginfo__init_offline_dwarf(struct debuginfo *dbg,
 	if (!dbg->dwfl)
 		goto error;
 
+	dwfl_report_begin(dbg->dwfl);
 	dbg->mod = dwfl_report_offline(dbg->dwfl, "", "", fd);
 	if (!dbg->mod)
 		goto error;
@@ -78,6 +79,8 @@ static int debuginfo__init_offline_dwarf(struct debuginfo *dbg,
 	if (!dbg->dbg)
 		goto error;
 
+	dwfl_report_end(dbg->dwfl, NULL, NULL);
+
 	return 0;
 error:
 	if (dbg->dwfl)
#7 lore raw

PATCH 4/5 perf-probe: Fix a segfault bug in debuginfo_cache

In reply to Masami Hiramatsu

perf probe --list will get a segfault if the first kprobe event is on a module and the second or latter one on the kernel.

e.g.
----

  # ./perf probe -q -m pcspkr pcspkr_event
  # ./perf probe -q vfs_read
  # ./perf probe -l
  Segmentation fault (core dumped)
  ----

This is because the debuginfo_cache fails to handle NULL module name, which causes segfault on strcmp. (Note that strcmp("something", NULL) always causes segfault)

To fix this debuginfo_cache__open always translates the NULL module name to "kernel" (this is correct, because NULL module name means opening the debuginfo for the kernel)

----

  # ./perf probe -l
    probe:pcspkr_event   (on pcspkr_event@drivers/input/misc/pcspkr.c
    in pcspkr)
    probe:vfs_read       (on vfs_read@ksrc/linux-3/fs/read_write.c)
  ----
---
 tools/perf/util/probe-event.c |   21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)
tools/perf/util/probe-event.c +12 -9
index 57a8cad..bb7766ea 100644
@@ -440,19 +440,22 @@ static char *debuginfo_cache_path;
 
 static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
 {
-	if ((debuginfo_cache_path && !strcmp(debuginfo_cache_path, module)) ||
-	    (!debuginfo_cache_path && !module && debuginfo_cache))
+	const char *path = module;
+
+	/* If the module is NULL, it should be the kernel. */
+	if (!module)
+		path = "kernel";
+
+	if (debuginfo_cache_path && !strcmp(debuginfo_cache_path, path))
 		goto out;
 
 	/* Copy module path */
 	free(debuginfo_cache_path);
-	if (module) {
-		debuginfo_cache_path = strdup(module);
-		if (!debuginfo_cache_path) {
-			debuginfo__delete(debuginfo_cache);
-			debuginfo_cache = NULL;
-			goto out;
-		}
+	debuginfo_cache_path = strdup(path);
+	if (!debuginfo_cache_path) {
+		debuginfo__delete(debuginfo_cache);
+		debuginfo_cache = NULL;
+		goto out;
 	}
 
 	debuginfo_cache = open_debuginfo(module, silent);
#9 lore raw

PATCH 5/5 perf-probe: Improve error message when %return is on inlined function

In reply to Masami Hiramatsu

perf probe shows more precisely message when it finds given %return target function is inlined.

Without this fix:
----

  # ./perf probe -V getname_flags%return
  Return probe must be on the head of a real function.
  Debuginfo analysis failed.
    Error: Failed to show vars.
  ----

With this fix:
----

  # ./perf probe -V getname_flags%return
  Failed to find "getname_flags%return",
   because getname_flags is an inlined function and has no return point.
  Debuginfo analysis failed.
    Error: Failed to show vars.
  ----
---
 tools/perf/util/probe-finder.c |   13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
tools/perf/util/probe-finder.c +9 -4
index f0708ff..bd8f03d 100644
@@ -594,6 +594,7 @@ static int find_variable(Dwarf_Die *sc_die, struct probe_finder *pf)
 /* Convert subprogram DIE to trace point */
 static int convert_to_trace_point(Dwarf_Die *sp_die, Dwfl_Module *mod,
 				  Dwarf_Addr paddr, bool retprobe,
+				  const char *function,
 				  struct probe_trace_point *tp)
 {
 	Dwarf_Addr eaddr, highaddr;
@@ -637,8 +638,10 @@ static int convert_to_trace_point(Dwarf_Die *sp_die, Dwfl_Module *mod,
 	/* Return probe must be on the head of a subprogram */
 	if (retprobe) {
 		if (eaddr != paddr) {
-			pr_warning("Return probe must be on the head of"
-				   " a real function.\n");
+			pr_warning("Failed to find \"%s%%return\",\n"
+				   " because %s is an inlined function and"
+				   " has no return point.\n", function,
+				   function);
 			return -EINVAL;
 		}
 		tp->retprobe = true;
@@ -1178,6 +1181,7 @@ static int add_probe_trace_event(Dwarf_Die *sc_die, struct probe_finder *pf)
 {
 	struct trace_event_finder *tf =
 			container_of(pf, struct trace_event_finder, pf);
+	struct perf_probe_point *pp = &pf->pev->point;
 	struct probe_trace_event *tev;
 	struct perf_probe_arg *args;
 	int ret, i;
@@ -1192,7 +1196,7 @@ static int add_probe_trace_event(Dwarf_Die *sc_die, struct probe_finder *pf)
 
 	/* Trace point should be converted from subprogram DIE */
 	ret = convert_to_trace_point(&pf->sp_die, tf->mod, pf->addr,
-				     pf->pev->point.retprobe, &tev->point);
+				     pp->retprobe, pp->function, &tev->point);
 	if (ret < 0)
 		return ret;
 
@@ -1322,6 +1326,7 @@ static int add_available_vars(Dwarf_Die *sc_die, struct probe_finder *pf)
 {
 	struct available_var_finder *af =
 			container_of(pf, struct available_var_finder, pf);
+	struct perf_probe_point *pp = &pf->pev->point;
 	struct variable_list *vl;
 	Dwarf_Die die_mem;
 	int ret;
@@ -1335,7 +1340,7 @@ static int add_available_vars(Dwarf_Die *sc_die, struct probe_finder *pf)
 
 	/* Trace point should be converted from subprogram DIE */
 	ret = convert_to_trace_point(&pf->sp_die, af->mod, pf->addr,
-				     pf->pev->point.retprobe, &vl->point);
+				     pp->retprobe, pp->function, &vl->point);
 	if (ret < 0)
 		return ret;
 
#5 lore raw

PATCH 3/5 perf-probe: Show correct source lines of probes on kmodules

In reply to Masami Hiramatsu

Perf probe always failed to find appropriate line numbers because of failing to find .text start address offset from debuginfo.

e.g.
----

  # ./perf probe -m pcspkr pcspkr_event:5
  Added new events:
    probe:pcspkr_event   (on pcspkr_event:5 in pcspkr)
    probe:pcspkr_event_1 (on pcspkr_event:5 in pcspkr)

You can now use it in all perf tools, such as:

perf record -e probe:pcspkr_event_1 -aR sleep 1

  # ./perf probe -l
  Failed to find debug information for address ffffffffa031f006
  Failed to find debug information for address ffffffffa031f016
    probe:pcspkr_event   (on pcspkr_event+6 in pcspkr)
    probe:pcspkr_event_1 (on pcspkr_event+22 in pcspkr)
  ----

This fixes the above issue as below.
1. Get the relative address of the symbol in .text by using map->start.
2. Adjust the address by adding the offset of .text section in the kernel module binary.

With this fix, perf probe -l shows lines correctly.
----

  # ./perf probe -l
    probe:pcspkr_event   (on pcspkr_event:5@drivers/input/misc/pcspkr.c
  in pcspkr)
    probe:pcspkr_event_1 (on pcspkr_event:5@drivers/input/misc/pcspkr.c
  in pcspkr)
  ----
---
 tools/perf/util/probe-event.c  |   35 ++++++++++++++++++++++-----------
 tools/perf/util/probe-finder.c |   42 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 65 insertions(+), 12 deletions(-)
tools/perf/util/probe-event.c +23 -12
index 7eb0239..57a8cad 100644
@@ -136,7 +136,8 @@ static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void)
 	return kmap->ref_reloc_sym;
 }
 
-static u64 kernel_get_symbol_address_by_name(const char *name, bool reloc)
+static int kernel_get_symbol_address_by_name(const char *name, u64 *addr,
+					     bool reloc, bool reladdr)
 {
 	struct ref_reloc_sym *reloc_sym;
 	struct symbol *sym;
@@ -145,12 +146,14 @@ static u64 kernel_get_symbol_address_by_name(const char *name, bool reloc)
 	/* ref_reloc_sym is just a label. Need a special fix*/
 	reloc_sym = kernel_get_ref_reloc_sym();
 	if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
-		return (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
+		*addr = (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
 	else {
 		sym = __find_kernel_function_by_name(name, &map);
-		if (sym)
-			return map->unmap_ip(map, sym->start) -
-				((reloc) ? 0 : map->reloc);
+		if (!sym)
+			return -ENOENT;
+		*addr = map->unmap_ip(map, sym->start) -
+			((reloc) ? 0 : map->reloc) -
+			((reladdr) ? map->start : 0);
 	}
 	return 0;
 }
@@ -244,12 +247,14 @@ static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
 static bool kprobe_blacklist__listed(unsigned long address);
 static bool kprobe_warn_out_range(const char *symbol, unsigned long address)
 {
-	u64 etext_addr;
+	u64 etext_addr = 0;
+	int ret;
 
 	/* Get the address of _etext for checking non-probable text symbol */
-	etext_addr = kernel_get_symbol_address_by_name("_etext", false);
+	ret = kernel_get_symbol_address_by_name("_etext", &etext_addr,
+						false, false);
 
-	if (etext_addr != 0 && etext_addr < address)
+	if (ret == 0 && etext_addr < address)
 		pr_warning("%s is out of .text, skip it.\n", symbol);
 	else if (kprobe_blacklist__listed(address))
 		pr_warning("%s is blacklisted function, skip it.\n", symbol);
@@ -516,8 +521,10 @@ static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
 			goto error;
 		addr += stext;
 	} else if (tp->symbol) {
-		addr = kernel_get_symbol_address_by_name(tp->symbol, false);
-		if (addr == 0)
+		/* If the module is given, this returns relative address */
+		ret = kernel_get_symbol_address_by_name(tp->symbol, &addr,
+							false, !!tp->module);
+		if (ret != 0)
 			goto error;
 		addr += tp->offset;
 	}
@@ -1883,8 +1890,12 @@ static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
 			goto out;
 		sym = map__find_symbol(map, addr, NULL);
 	} else {
-		if (tp->symbol)
-			addr = kernel_get_symbol_address_by_name(tp->symbol, true);
+		if (tp->symbol && !addr) {
+			ret = kernel_get_symbol_address_by_name(tp->symbol,
+							&addr, true, false);
+			if (ret < 0)
+				goto out;
+		}
 		if (addr) {
 			addr += tp->offset;
 			sym = __find_kernel_function(addr, &map);
tools/perf/util/probe-finder.c +42 -0

57 lines not on this page · raw message

#3 lore raw

PATCH 2/5 perf-probe: Fix to remove dot suffix from second or latter events

In reply to Masami Hiramatsu

Fix to remove dot suffix (e.g. .const, .isra) from the second or latter events which has suffix numbers.

Since the previous commit 35a23ff928b0 ("perf probe: Cut
off the gcc optimization postfixes from function name")
didn't care about the suffix numbered events, therefore we'll have an error when we add additional events on the same dot suffix functions.

e.g.
----

  # ./perf probe -f -a get_sigframe.isra.2.constprop.3 \
   -a get_sigframe.isra.2.constprop.3
  Failed to write event: Invalid argument
    Error: Failed to add events.
  ----

This fixes above issue as below:
----

  # ./perf probe -f -a get_sigframe.isra.2.constprop.3 \
   -a get_sigframe.isra.2.constprop.3
  Added new events:
    probe:get_sigframe   (on get_sigframe.isra.2.constprop.3)
    probe:get_sigframe_1 (on get_sigframe.isra.2.constprop.3)

You can now use it in all perf tools, such as:

perf record -e probe:get_sigframe_1 -aR sleep 1

----

---
 tools/perf/util/probe-event.c |   33 ++++++++++++++++++++-------------
 1 file changed, 20 insertions(+), 13 deletions(-)
tools/perf/util/probe-event.c +20 -13
index 7fb0533..7eb0239 100644
@@ -2288,36 +2288,41 @@ static int get_new_event_name(char *buf, size_t len, const char *base,
 			      struct strlist *namelist, bool allow_suffix)
 {
 	int i, ret;
-	char *p;
+	char *p, *nbase;
 
 	if (*base == '.')
 		base++;
+	nbase = strdup(base);
+	if (!nbase)
+		return -ENOMEM;
+
+	/* Cut off the dot suffixes (e.g. .const, .isra)*/
+	p = strchr(nbase, '.');
+	if (p && p != nbase)
+		*p = '\0';
 
-	/* Try no suffix */
-	ret = e_snprintf(buf, len, "%s", base);
+	/* Try no suffix number */
+	ret = e_snprintf(buf, len, "%s", nbase);
 	if (ret < 0) {
 		pr_debug("snprintf() failed: %d\n", ret);
-		return ret;
+		goto out;
 	}
-	/* Cut off the postfixes (e.g. .const, .isra)*/
-	p = strchr(buf, '.');
-	if (p && p != buf)
-		*p = '\0';
 	if (!strlist__has_entry(namelist, buf))
-		return 0;
+		goto out;
 
 	if (!allow_suffix) {
 		pr_warning("Error: event \"%s\" already exists. "
-			   "(Use -f to force duplicates.)\n", base);
-		return -EEXIST;
+			   "(Use -f to force duplicates.)\n", buf);
+		ret = -EEXIST;
+		goto out;
 	}
 
 	/* Try to add suffix */
 	for (i = 1; i < MAX_EVENT_INDEX; i++) {
-		ret = e_snprintf(buf, len, "%s_%d", base, i);
+		ret = e_snprintf(buf, len, "%s_%d", nbase, i);
 		if (ret < 0) {
 			pr_debug("snprintf() failed: %d\n", ret);
-			return ret;
+			goto out;
 		}
 		if (!strlist__has_entry(namelist, buf))
 			break;
@@ -2327,6 +2332,8 @@ static int get_new_event_name(char *buf, size_t len, const char *base,
 		ret = -ERANGE;
 	}
 
+out:
+	free(nbase);
 	return ret;
 }
 
#11 lore raw
In reply to Masami Hiramatsu

Em Thu, Oct 01, 2015 at 01:41:26AM +0900, Masami Hiramatsu escreveu:

Quoted 4 lines

Hi,

Here is a bunch of bugfix patches for perf-probe.

Quoted 12 lines

- Begin and end report session correctly, which fixes a mistake of libdwfl usage.
- Fix to remove dot suffix from second or latter events which fixes a bug on multiple events on a dot suffixed symbol.
- Show correct source lines of probes on kmodules, which fixes a bug that perf-probe --list can not find probe points in kernel modules.
- Fix a segfault bug in debuginfo_cache, which fixes a segfault bug in debuginfo_cache__open.
- Improve error message when %return is on inlined function is not actual bugfix, but improve an error message.

Thanks, did a really quick review, tested and applied.

- Arnaldo

#4 lore raw

[tip:perf/core] perf probe: Fix to remove dot suffix from second or latter events

In reply to Masami Hiramatsu
perf probe: Fix to remove dot suffix from second or latter events
Commit-ID:  663b1151f2d674e2004c015cbe9995749033c49a
Gitweb:     http://git.kernel.org/tip/663b1151f2d674e2004c015cbe9995749033c49a
Author:     Masami Hiramatsu <[email protected]>
AuthorDate: Thu, 1 Oct 2015 01:41:30 +0900
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Wed, 30 Sep 2015 18:34:33 -0300

perf probe: Fix to remove dot suffix from second or latter events

Fix to remove dot suffix (e.g. .const, .isra) from the second or latter events which has suffix numbers.

Since the previous commit 35a23ff928b0 ("perf probe: Cut off the gcc optimization postfixes from function name") didn't care about the suffix numbered events, therefore we'll have an error when we add additional events on the same dot suffix functions.

e.g.
----

  # ./perf probe -f -a get_sigframe.isra.2.constprop.3 \
   -a get_sigframe.isra.2.constprop.3
  Failed to write event: Invalid argument
    Error: Failed to add events.
  ----

This fixes above issue as below:
----

  # ./perf probe -f -a get_sigframe.isra.2.constprop.3 \
   -a get_sigframe.isra.2.constprop.3
  Added new events:
    probe:get_sigframe   (on get_sigframe.isra.2.constprop.3)
    probe:get_sigframe_1 (on get_sigframe.isra.2.constprop.3)

You can now use it in all perf tools, such as:

perf record -e probe:get_sigframe_1 -aR sleep 1

----

---
 tools/perf/util/probe-event.c | 33 ++++++++++++++++++++-------------
 1 file changed, 20 insertions(+), 13 deletions(-)
tools/perf/util/probe-event.c +20 -13
index 8b9a680..5d68f68 100644
@@ -2289,36 +2289,41 @@ static int get_new_event_name(char *buf, size_t len, const char *base,
 			      struct strlist *namelist, bool allow_suffix)
 {
 	int i, ret;
-	char *p;
+	char *p, *nbase;
 
 	if (*base == '.')
 		base++;
+	nbase = strdup(base);
+	if (!nbase)
+		return -ENOMEM;
+
+	/* Cut off the dot suffixes (e.g. .const, .isra)*/
+	p = strchr(nbase, '.');
+	if (p && p != nbase)
+		*p = '\0';
 
-	/* Try no suffix */
-	ret = e_snprintf(buf, len, "%s", base);
+	/* Try no suffix number */
+	ret = e_snprintf(buf, len, "%s", nbase);
 	if (ret < 0) {
 		pr_debug("snprintf() failed: %d\n", ret);
-		return ret;
+		goto out;
 	}
-	/* Cut off the postfixes (e.g. .const, .isra)*/
-	p = strchr(buf, '.');
-	if (p && p != buf)
-		*p = '\0';
 	if (!strlist__has_entry(namelist, buf))
-		return 0;
+		goto out;
 
 	if (!allow_suffix) {
 		pr_warning("Error: event \"%s\" already exists. "
-			   "(Use -f to force duplicates.)\n", base);
-		return -EEXIST;
+			   "(Use -f to force duplicates.)\n", buf);
+		ret = -EEXIST;
+		goto out;
 	}
 
 	/* Try to add suffix */
 	for (i = 1; i < MAX_EVENT_INDEX; i++) {
-		ret = e_snprintf(buf, len, "%s_%d", base, i);
+		ret = e_snprintf(buf, len, "%s_%d", nbase, i);
 		if (ret < 0) {
 			pr_debug("snprintf() failed: %d\n", ret);
-			return ret;
+			goto out;
 		}
 		if (!strlist__has_entry(namelist, buf))
 			break;
@@ -2328,6 +2333,8 @@ static int get_new_event_name(char *buf, size_t len, const char *base,
 		ret = -ERANGE;
 	}
 
+out:
+	free(nbase);
 	return ret;
 }
 
#2 lore raw

[tip:perf/core] perf probe: Begin and end libdwfl report session correctly

In reply to Masami Hiramatsu
perf probe: Begin and end libdwfl report session correctly
Commit-ID:  9135949ddd9d0d8d73a2f441912508d25a4a82a2
Gitweb:     http://git.kernel.org/tip/9135949ddd9d0d8d73a2f441912508d25a4a82a2
Author:     Masami Hiramatsu <[email protected]>
AuthorDate: Thu, 1 Oct 2015 01:41:28 +0900
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Wed, 30 Sep 2015 18:34:34 -0300

perf probe: Begin and end libdwfl report session correctly

Fix a trival bug about libdwfl usage of the report session, it should explicitly begin and end a report session around dwfl_report_offline().

---
 tools/perf/util/probe-finder.c | 3 +++
 1 file changed, 3 insertions(+)
tools/perf/util/probe-finder.c +3 -0
index 29c43c068..35f905f 100644
@@ -70,6 +70,7 @@ static int debuginfo__init_offline_dwarf(struct debuginfo *dbg,
 	if (!dbg->dwfl)
 		goto error;
 
+	dwfl_report_begin(dbg->dwfl);
 	dbg->mod = dwfl_report_offline(dbg->dwfl, "", "", fd);
 	if (!dbg->mod)
 		goto error;
@@ -78,6 +79,8 @@ static int debuginfo__init_offline_dwarf(struct debuginfo *dbg,
 	if (!dbg->dbg)
 		goto error;
 
+	dwfl_report_end(dbg->dwfl, NULL, NULL);
+
 	return 0;
 error:
 	if (dbg->dwfl)
#6 lore raw

[tip:perf/core] perf probe: Show correct source lines of probes on kmodules

In reply to Masami Hiramatsu
perf probe: Show correct source lines of probes on kmodules
Commit-ID:  9b239a12bc872f212cefbaee4d028c838abe6ff3
Gitweb:     http://git.kernel.org/tip/9b239a12bc872f212cefbaee4d028c838abe6ff3
Author:     Masami Hiramatsu <[email protected]>
AuthorDate: Thu, 1 Oct 2015 01:41:33 +0900
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Wed, 30 Sep 2015 18:34:35 -0300

perf probe: Show correct source lines of probes on kmodules

Perf probe always failed to find appropriate line numbers because of failing to find .text start address offset from debuginfo.

e.g.
----

  # ./perf probe -m pcspkr pcspkr_event:5
  Added new events:
    probe:pcspkr_event   (on pcspkr_event:5 in pcspkr)
    probe:pcspkr_event_1 (on pcspkr_event:5 in pcspkr)

You can now use it in all perf tools, such as:

perf record -e probe:pcspkr_event_1 -aR sleep 1

  # ./perf probe -l
  Failed to find debug information for address ffffffffa031f006
  Failed to find debug information for address ffffffffa031f016
    probe:pcspkr_event   (on pcspkr_event+6 in pcspkr)
    probe:pcspkr_event_1 (on pcspkr_event+22 in pcspkr)
  ----

This fixes the above issue as below.
1. Get the relative address of the symbol in .text by using map->start.
2. Adjust the address by adding the offset of .text section in the kernel module binary.

With this fix, perf probe -l shows lines correctly.
----

  # ./perf probe -l
    probe:pcspkr_event   (on pcspkr_event:5@drivers/input/misc/pcspkr.c in pcspkr)
    probe:pcspkr_event_1 (on pcspkr_event:5@drivers/input/misc/pcspkr.c in pcspkr)
  ----
---
 tools/perf/util/probe-event.c  | 35 +++++++++++++++++++++++------------
 tools/perf/util/probe-finder.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 65 insertions(+), 12 deletions(-)
tools/perf/util/probe-event.c +23 -12
index 5d68f68..65be284 100644
@@ -137,7 +137,8 @@ static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void)
 	return kmap->ref_reloc_sym;
 }
 
-static u64 kernel_get_symbol_address_by_name(const char *name, bool reloc)
+static int kernel_get_symbol_address_by_name(const char *name, u64 *addr,
+					     bool reloc, bool reladdr)
 {
 	struct ref_reloc_sym *reloc_sym;
 	struct symbol *sym;
@@ -146,12 +147,14 @@ static u64 kernel_get_symbol_address_by_name(const char *name, bool reloc)
 	/* ref_reloc_sym is just a label. Need a special fix*/
 	reloc_sym = kernel_get_ref_reloc_sym();
 	if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
-		return (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
+		*addr = (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
 	else {
 		sym = __find_kernel_function_by_name(name, &map);
-		if (sym)
-			return map->unmap_ip(map, sym->start) -
-				((reloc) ? 0 : map->reloc);
+		if (!sym)
+			return -ENOENT;
+		*addr = map->unmap_ip(map, sym->start) -
+			((reloc) ? 0 : map->reloc) -
+			((reladdr) ? map->start : 0);
 	}
 	return 0;
 }
@@ -245,12 +248,14 @@ static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
 static bool kprobe_blacklist__listed(unsigned long address);
 static bool kprobe_warn_out_range(const char *symbol, unsigned long address)
 {
-	u64 etext_addr;
+	u64 etext_addr = 0;
+	int ret;
 
 	/* Get the address of _etext for checking non-probable text symbol */
-	etext_addr = kernel_get_symbol_address_by_name("_etext", false);
+	ret = kernel_get_symbol_address_by_name("_etext", &etext_addr,
+						false, false);
 
-	if (etext_addr != 0 && etext_addr < address)
+	if (ret == 0 && etext_addr < address)
 		pr_warning("%s is out of .text, skip it.\n", symbol);
 	else if (kprobe_blacklist__listed(address))
 		pr_warning("%s is blacklisted function, skip it.\n", symbol);
@@ -517,8 +522,10 @@ static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
 			goto error;
 		addr += stext;
 	} else if (tp->symbol) {
-		addr = kernel_get_symbol_address_by_name(tp->symbol, false);
-		if (addr == 0)
+		/* If the module is given, this returns relative address */
+		ret = kernel_get_symbol_address_by_name(tp->symbol, &addr,
+							false, !!tp->module);
+		if (ret != 0)
 			goto error;
 		addr += tp->offset;
 	}
@@ -1884,8 +1891,12 @@ static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
 			goto out;
 		sym = map__find_symbol(map, addr, NULL);
 	} else {
-		if (tp->symbol)
-			addr = kernel_get_symbol_address_by_name(tp->symbol, true);
+		if (tp->symbol && !addr) {
+			ret = kernel_get_symbol_address_by_name(tp->symbol,
+							&addr, true, false);
+			if (ret < 0)
+				goto out;
+		}
 		if (addr) {
 			addr += tp->offset;
 			sym = __find_kernel_function(addr, &map);
tools/perf/util/probe-finder.c +42 -0

57 lines not on this page · raw message

#8 lore raw

[tip:perf/core] perf probe: Fix a segfault bug in debuginfo_cache

In reply to Masami Hiramatsu
perf probe: Fix a segfault bug in debuginfo_cache
Commit-ID:  20f49859c785183d5296670a10dace454131274b
Gitweb:     http://git.kernel.org/tip/20f49859c785183d5296670a10dace454131274b
Author:     Masami Hiramatsu <[email protected]>
AuthorDate: Thu, 1 Oct 2015 01:41:35 +0900
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Wed, 30 Sep 2015 18:34:36 -0300

perf probe: Fix a segfault bug in debuginfo_cache

perf probe --list will get a segfault if the first kprobe event is on a module and the second or latter one is on the kernel.

e.g.
----

  # ./perf probe -q -m pcspkr pcspkr_event
  # ./perf probe -q vfs_read
  # ./perf probe -l
  Segmentation fault (core dumped)
  ----

This is because the debuginfo_cache fails to handle NULL module name, which causes segfault on strcmp. (Note that strcmp("something", NULL) always causes segfault)

To fix this debuginfo_cache__open always translates the NULL module name to "kernel" (this is correct, because NULL module name means opening the debuginfo for the kernel)

----

  # ./perf probe -l
    probe:pcspkr_event   (on pcspkr_event@drivers/input/misc/pcspkr.c
    in pcspkr)
    probe:vfs_read       (on vfs_read@ksrc/linux-3/fs/read_write.c)
  ----
---
 tools/perf/util/probe-event.c | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)
tools/perf/util/probe-event.c +12 -9
index 65be284..3010abc 100644
@@ -441,19 +441,22 @@ static char *debuginfo_cache_path;
 
 static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
 {
-	if ((debuginfo_cache_path && !strcmp(debuginfo_cache_path, module)) ||
-	    (!debuginfo_cache_path && !module && debuginfo_cache))
+	const char *path = module;
+
+	/* If the module is NULL, it should be the kernel. */
+	if (!module)
+		path = "kernel";
+
+	if (debuginfo_cache_path && !strcmp(debuginfo_cache_path, path))
 		goto out;
 
 	/* Copy module path */
 	free(debuginfo_cache_path);
-	if (module) {
-		debuginfo_cache_path = strdup(module);
-		if (!debuginfo_cache_path) {
-			debuginfo__delete(debuginfo_cache);
-			debuginfo_cache = NULL;
-			goto out;
-		}
+	debuginfo_cache_path = strdup(path);
+	if (!debuginfo_cache_path) {
+		debuginfo__delete(debuginfo_cache);
+		debuginfo_cache = NULL;
+		goto out;
 	}
 
 	debuginfo_cache = open_debuginfo(module, silent);
#10 lore raw

[tip:perf/core] perf probe: Improve error message when %return is on inlined function

In reply to Masami Hiramatsu
perf probe: Improve error message when %return is on inlined function
Commit-ID:  6cca13bdf5a5d561a855259689874e0c7266eec3
Gitweb:     http://git.kernel.org/tip/6cca13bdf5a5d561a855259689874e0c7266eec3
Author:     Masami Hiramatsu <[email protected]>
AuthorDate: Thu, 1 Oct 2015 01:41:37 +0900
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Wed, 30 Sep 2015 18:34:37 -0300

perf probe: Improve error message when %return is on inlined function

perf probe shows more precisely message when it finds given %return target function is inlined.

Without this fix:
----

  # ./perf probe -V getname_flags%return
  Return probe must be on the head of a real function.
  Debuginfo analysis failed.
    Error: Failed to show vars.
  ----

With this fix:
----

  # ./perf probe -V getname_flags%return
  Failed to find "getname_flags%return",
   because getname_flags is an inlined function and has no return point.
  Debuginfo analysis failed.
    Error: Failed to show vars.
  ----
---
 tools/perf/util/probe-finder.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
tools/perf/util/probe-finder.c +9 -4
index f0708ff..bd8f03d 100644
@@ -594,6 +594,7 @@ static int find_variable(Dwarf_Die *sc_die, struct probe_finder *pf)
 /* Convert subprogram DIE to trace point */
 static int convert_to_trace_point(Dwarf_Die *sp_die, Dwfl_Module *mod,
 				  Dwarf_Addr paddr, bool retprobe,
+				  const char *function,
 				  struct probe_trace_point *tp)
 {
 	Dwarf_Addr eaddr, highaddr;
@@ -637,8 +638,10 @@ static int convert_to_trace_point(Dwarf_Die *sp_die, Dwfl_Module *mod,
 	/* Return probe must be on the head of a subprogram */
 	if (retprobe) {
 		if (eaddr != paddr) {
-			pr_warning("Return probe must be on the head of"
-				   " a real function.\n");
+			pr_warning("Failed to find \"%s%%return\",\n"
+				   " because %s is an inlined function and"
+				   " has no return point.\n", function,
+				   function);
 			return -EINVAL;
 		}
 		tp->retprobe = true;
@@ -1178,6 +1181,7 @@ static int add_probe_trace_event(Dwarf_Die *sc_die, struct probe_finder *pf)
 {
 	struct trace_event_finder *tf =
 			container_of(pf, struct trace_event_finder, pf);
+	struct perf_probe_point *pp = &pf->pev->point;
 	struct probe_trace_event *tev;
 	struct perf_probe_arg *args;
 	int ret, i;
@@ -1192,7 +1196,7 @@ static int add_probe_trace_event(Dwarf_Die *sc_die, struct probe_finder *pf)
 
 	/* Trace point should be converted from subprogram DIE */
 	ret = convert_to_trace_point(&pf->sp_die, tf->mod, pf->addr,
-				     pf->pev->point.retprobe, &tev->point);
+				     pp->retprobe, pp->function, &tev->point);
 	if (ret < 0)
 		return ret;
 
@@ -1322,6 +1326,7 @@ static int add_available_vars(Dwarf_Die *sc_die, struct probe_finder *pf)
 {
 	struct available_var_finder *af =
 			container_of(pf, struct available_var_finder, pf);
+	struct perf_probe_point *pp = &pf->pev->point;
 	struct variable_list *vl;
 	Dwarf_Die die_mem;
 	int ret;
@@ -1335,7 +1340,7 @@ static int add_available_vars(Dwarf_Die *sc_die, struct probe_finder *pf)
 
 	/* Trace point should be converted from subprogram DIE */
 	ret = convert_to_trace_point(&pf->sp_die, af->mod, pf->addr,
-				     pf->pev->point.retprobe, &vl->point);
+				     pp->retprobe, pp->function, &vl->point);
 	if (ret < 0)
 		return ret;