* [PATCH] perf: fix dwarf unwind using libunwind.
@ 2015-01-13 2:54 Wang Nan
2015-01-13 7:10 ` Jiri Olsa
0 siblings, 1 reply; 8+ messages in thread
From: Wang Nan @ 2015-01-13 2:54 UTC (permalink / raw)
To: jolsa, namhyung, a.p.zijlstra, paulus, mingo, acme,
masami.hiramatsu.pt, jean.pihet
Cc: linux-kernel, lizefan
Original perf tool fails to unwind user stack if the event is issued in
a shared object:
$ perf record -e syscalls:sys_enter_write -g --call-graph=dwarf babeltrace
$ perf report --stdio
# To display the perf.data header info, please use --header/--header-only options.
#
# Samples: 37 of event 'syscalls:sys_enter_write'
# Event count (approx.): 37
#
# Children Self Command Shared Object Symbol
# ........ ........ .......... ............. .....................
#
100.00% 100.00% babeltrace libc-2.18.so [.] __GI___libc_write
|
---__GI___libc_write
By debugging libunwind I found that there is a bug in unwind-libunwind:
it always passes 0 as segbase to libunwind, cause libunwind unable to
locate debug_frame entry fir first level ip address (I add some more
debugging output into libunwind to make things clear):
>_Uarm_dwarf_find_debug_frame: start_ip = 10be98, end_ip = 10c2a4
>_Uarm_dwarf_find_debug_frame: found debug_frame table `/lib/libc-2.18.so': segbase=0x0, len=7, gp=0x0, table_data=0x449388
>_Uarm_dwarf_search_unwind_table: call lookup:ip = b6cd3bcc, segbase = 0, rel_ip = b6cd3bcc
>lookup: e->start_ip_offset = bcf18 (rel_ip = b6cd3bcc)
>lookup: e->start_ip_offset = 6d314 (rel_ip = b6cd3bcc)
>lookup: e->start_ip_offset = 33d0c (rel_ip = b6cd3bcc)
>lookup: e->start_ip_offset = 2ad6c (rel_ip = b6cd3bcc)
>lookup: e->start_ip_offset = 23004 (rel_ip = b6cd3bcc)
>lookup: e->start_ip_offset = 18f28 (rel_ip = b6cd3bcc)
>lookup: e->start_ip_offset = 184e8 (rel_ip = b6cd3bcc)
>lookup: e->start_ip_offset = 167cc (rel_ip = b6cd3bcc)
>lookup: e->start_ip_offset = 16294 (rel_ip = b6cd3bcc)
>lookup: e->start_ip_offset = 15d88 (rel_ip = b6cd3bcc)
>lookup: e->start_ip_offset = 15d0c (rel_ip = b6cd3bcc)
>lookup: e->start_ip_offset = 15c40 (rel_ip = b6cd3bcc)
>_Uarm_dwarf_search_unwind_table: IP b6cd3bcc inside range b6c12000-b6d4c000, but no explicit unwind info found
>put_rs_cache: unmasking signals/interrupts and releasing lock
>_Uarm_dwarf_step: returning -10
>_Uarm_step: dwarf_step()=-10
This patch passes map->start as segbase to dwarf_find_debug_frame(), so
di will be initialized correctly.
In addition, dso and executable are different when setting segbase. This
patch first check whether the elf is executable, and pass segbase only
for shared object.
Signed-off-by: Wang Nan <wangnan0@huawei.com>
---
tools/perf/util/unwind-libunwind.c | 27 ++++++++++++++++++++++++++-
1 file changed, 26 insertions(+), 1 deletion(-)
diff --git a/tools/perf/util/unwind-libunwind.c b/tools/perf/util/unwind-libunwind.c
index 371219a..32040fa 100644
--- a/tools/perf/util/unwind-libunwind.c
+++ b/tools/perf/util/unwind-libunwind.c
@@ -185,6 +185,27 @@ static u64 elf_section_offset(int fd, const char *name)
return offset;
}
+static int elf_is_exec(int fd, const char *name)
+{
+ Elf *elf;
+ GElf_Ehdr ehdr;
+ int retval = 0;
+
+ elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
+ if (elf == NULL)
+ return 0;
+ if (gelf_getehdr(elf, &ehdr) == NULL)
+ goto out;
+
+ retval = (ehdr.e_type == ET_EXEC);
+
+out:
+ elf_end(elf);
+ pr_debug("unwind: elf_is_exec(%s): %d\n", name, retval);
+ return retval;
+
+}
+
struct table_entry {
u32 start_ip_offset;
u32 fde_offset;
@@ -322,8 +343,12 @@ find_proc_info(unw_addr_space_t as, unw_word_t ip, unw_proc_info_t *pi,
#ifndef NO_LIBUNWIND_DEBUG_FRAME
/* Check the .debug_frame section for unwinding info */
if (!read_unwind_spec_debug_frame(map->dso, ui->machine, &segbase)) {
+ int fd = dso__data_fd(map->dso, ui->machine);
+ int is_exec = elf_is_exec(fd, map->dso->name);
+ unw_word_t base = is_exec ? 0 : map->start;
+
memset(&di, 0, sizeof(di));
- if (dwarf_find_debug_frame(0, &di, ip, 0, map->dso->name,
+ if (dwarf_find_debug_frame(0, &di, ip, base, map->dso->name,
map->start, map->end))
return dwarf_search_unwind_table(as, ip, &di, pi,
need_unwind_info, arg);
--
1.8.4
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [PATCH] perf: fix dwarf unwind using libunwind. 2015-01-13 2:54 [PATCH] perf: fix dwarf unwind using libunwind Wang Nan @ 2015-01-13 7:10 ` Jiri Olsa 2015-01-13 8:36 ` Wang Nan 0 siblings, 1 reply; 8+ messages in thread From: Jiri Olsa @ 2015-01-13 7:10 UTC (permalink / raw) To: Wang Nan Cc: jolsa, namhyung, a.p.zijlstra, paulus, mingo, acme, masami.hiramatsu.pt, jean.pihet, linux-kernel, lizefan On Tue, Jan 13, 2015 at 10:54:37AM +0800, Wang Nan wrote: SNIP > > +static int elf_is_exec(int fd, const char *name) > +{ > + Elf *elf; > + GElf_Ehdr ehdr; > + int retval = 0; > + > + elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); > + if (elf == NULL) > + return 0; > + if (gelf_getehdr(elf, &ehdr) == NULL) > + goto out; > + > + retval = (ehdr.e_type == ET_EXEC); > + > +out: > + elf_end(elf); > + pr_debug("unwind: elf_is_exec(%s): %d\n", name, retval); > + return retval; > + > +} yuou need to add stub for 'NO_LIBUNWIND_DEBUG_FRAME=1', otherwise: CC util/dwarf-aux.o CC util/unwind-libunwind.o util/unwind-libunwind.c:188:12: error: ‘elf_is_exec’ defined but not used [-Werror=unused-function] static int elf_is_exec(int fd, const char *name) ^ cc1: all warnings being treated as errors make[1]: *** [util/unwind-libunwind.o] Error 1 make[1]: *** Waiting for unfinished jobs.... make: *** [all] Error 2 jirka > + > struct table_entry { > u32 start_ip_offset; > u32 fde_offset; > @@ -322,8 +343,12 @@ find_proc_info(unw_addr_space_t as, unw_word_t ip, unw_proc_info_t *pi, > #ifndef NO_LIBUNWIND_DEBUG_FRAME > /* Check the .debug_frame section for unwinding info */ > if (!read_unwind_spec_debug_frame(map->dso, ui->machine, &segbase)) { > + int fd = dso__data_fd(map->dso, ui->machine); > + int is_exec = elf_is_exec(fd, map->dso->name); > + unw_word_t base = is_exec ? 0 : map->start; > + > memset(&di, 0, sizeof(di)); > - if (dwarf_find_debug_frame(0, &di, ip, 0, map->dso->name, > + if (dwarf_find_debug_frame(0, &di, ip, base, map->dso->name, > map->start, map->end)) > return dwarf_search_unwind_table(as, ip, &di, pi, > need_unwind_info, arg); > -- > 1.8.4 > ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] perf: fix dwarf unwind using libunwind. 2015-01-13 7:10 ` Jiri Olsa @ 2015-01-13 8:36 ` Wang Nan 2015-01-13 9:19 ` Jiri Olsa 0 siblings, 1 reply; 8+ messages in thread From: Wang Nan @ 2015-01-13 8:36 UTC (permalink / raw) To: Jiri Olsa Cc: jolsa, namhyung, a.p.zijlstra, paulus, mingo, acme, masami.hiramatsu.pt, jean.pihet, linux-kernel, lizefan On 2015/1/13 15:10, Jiri Olsa wrote: > On Tue, Jan 13, 2015 at 10:54:37AM +0800, Wang Nan wrote: > > SNIP > >> >> +static int elf_is_exec(int fd, const char *name) >> +{ >> + Elf *elf; >> + GElf_Ehdr ehdr; >> + int retval = 0; >> + >> + elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); >> + if (elf == NULL) >> + return 0; >> + if (gelf_getehdr(elf, &ehdr) == NULL) >> + goto out; >> + >> + retval = (ehdr.e_type == ET_EXEC); >> + >> +out: >> + elf_end(elf); >> + pr_debug("unwind: elf_is_exec(%s): %d\n", name, retval); >> + return retval; >> + >> +} > > yuou need to add stub for 'NO_LIBUNWIND_DEBUG_FRAME=1', otherwise: > > CC util/dwarf-aux.o > CC util/unwind-libunwind.o > util/unwind-libunwind.c:188:12: error: ‘elf_is_exec’ defined but not used [-Werror=unused-function] > static int elf_is_exec(int fd, const char *name) > ^ > cc1: all warnings being treated as errors > make[1]: *** [util/unwind-libunwind.o] Error 1 > make[1]: *** Waiting for unfinished jobs.... > make: *** [all] Error 2 > > jirka > Thanks. I haven't test NO_LIBUNWIND_DEBUG_FRAME=1 case. I found that it is possible to utilize map->dso->adjust_symbols for this propose so we don't need to introduce another function to check ehdr.e_type. However map->dso->adjust_symbols checks not only ET_EXEC but also ET_REL. What do you think? I'd like to check it and post another patch. >> + >> struct table_entry { >> u32 start_ip_offset; >> u32 fde_offset; >> @@ -322,8 +343,12 @@ find_proc_info(unw_addr_space_t as, unw_word_t ip, unw_proc_info_t *pi, >> #ifndef NO_LIBUNWIND_DEBUG_FRAME >> /* Check the .debug_frame section for unwinding info */ >> if (!read_unwind_spec_debug_frame(map->dso, ui->machine, &segbase)) { >> + int fd = dso__data_fd(map->dso, ui->machine); >> + int is_exec = elf_is_exec(fd, map->dso->name); >> + unw_word_t base = is_exec ? 0 : map->start; >> + >> memset(&di, 0, sizeof(di)); >> - if (dwarf_find_debug_frame(0, &di, ip, 0, map->dso->name, >> + if (dwarf_find_debug_frame(0, &di, ip, base, map->dso->name, >> map->start, map->end)) >> return dwarf_search_unwind_table(as, ip, &di, pi, >> need_unwind_info, arg); >> -- >> 1.8.4 >> ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] perf: fix dwarf unwind using libunwind. 2015-01-13 8:36 ` Wang Nan @ 2015-01-13 9:19 ` Jiri Olsa 2015-01-14 2:36 ` [PATCH v2] " Wang Nan 0 siblings, 1 reply; 8+ messages in thread From: Jiri Olsa @ 2015-01-13 9:19 UTC (permalink / raw) To: Wang Nan Cc: jolsa, namhyung, a.p.zijlstra, paulus, mingo, acme, masami.hiramatsu.pt, jean.pihet, linux-kernel, lizefan On Tue, Jan 13, 2015 at 04:36:53PM +0800, Wang Nan wrote: > On 2015/1/13 15:10, Jiri Olsa wrote: > > On Tue, Jan 13, 2015 at 10:54:37AM +0800, Wang Nan wrote: > > > > SNIP > > > >> > >> +static int elf_is_exec(int fd, const char *name) > >> +{ > >> + Elf *elf; > >> + GElf_Ehdr ehdr; > >> + int retval = 0; > >> + > >> + elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); > >> + if (elf == NULL) > >> + return 0; > >> + if (gelf_getehdr(elf, &ehdr) == NULL) > >> + goto out; > >> + > >> + retval = (ehdr.e_type == ET_EXEC); > >> + > >> +out: > >> + elf_end(elf); > >> + pr_debug("unwind: elf_is_exec(%s): %d\n", name, retval); > >> + return retval; > >> + > >> +} > > > > yuou need to add stub for 'NO_LIBUNWIND_DEBUG_FRAME=1', otherwise: > > > > CC util/dwarf-aux.o > > CC util/unwind-libunwind.o > > util/unwind-libunwind.c:188:12: error: ‘elf_is_exec’ defined but not used [-Werror=unused-function] > > static int elf_is_exec(int fd, const char *name) > > ^ > > cc1: all warnings being treated as errors > > make[1]: *** [util/unwind-libunwind.o] Error 1 > > make[1]: *** Waiting for unfinished jobs.... > > make: *** [all] Error 2 > > > > jirka > > > > Thanks. I haven't test NO_LIBUNWIND_DEBUG_FRAME=1 case. > > I found that it is possible to utilize map->dso->adjust_symbols for this propose so > we don't need to introduce another function to check ehdr.e_type. However > map->dso->adjust_symbols checks not only ET_EXEC but also ET_REL. What do you think? > > I'd like to check it and post another patch. please also update tests/mate thanks, jirka ^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v2] perf: fix dwarf unwind using libunwind. 2015-01-13 9:19 ` Jiri Olsa @ 2015-01-14 2:36 ` Wang Nan 2015-01-14 11:57 ` Jiri Olsa 2015-01-17 10:12 ` [tip:perf/urgent] perf test: Fix " tip-bot for Wang Nan 0 siblings, 2 replies; 8+ messages in thread From: Wang Nan @ 2015-01-14 2:36 UTC (permalink / raw) To: jolsa Cc: linux-kernel, lizefan, namhyung, a.p.zijlstra, paulus, mingo, acme, masami.hiramatsu.pt Perf tool fails to unwind user stack if the event raises in a shared object. This patch improves tests/dwarf-unwind.c to demonstrate the problem by utilizing commonly used glibc function "bsearch". If perf is not statically linked, the testcase will try to unwind a mixed call trace. By debugging libunwind I found that there is a bug in unwind-libunwind: it always passes 0 as segbase to libunwind, cause libunwind unable to locate debug_frame entry fir first level ip address (I add some more debugging output into libunwind to make things clear): >_Uarm_dwarf_find_debug_frame: start_ip = 10be98, end_ip = 10c2a4 >_Uarm_dwarf_find_debug_frame: found debug_frame table `/lib/libc-2.18.so': segbase=0x0, len=7, gp=0x0, table_data=0x449388 >_Uarm_dwarf_search_unwind_table: call lookup:ip = b6cd3bcc, segbase = 0, rel_ip = b6cd3bcc >lookup: e->start_ip_offset = bcf18 (rel_ip = b6cd3bcc) >lookup: e->start_ip_offset = 6d314 (rel_ip = b6cd3bcc) >lookup: e->start_ip_offset = 33d0c (rel_ip = b6cd3bcc) ... >lookup: e->start_ip_offset = 15d0c (rel_ip = b6cd3bcc) >lookup: e->start_ip_offset = 15c40 (rel_ip = b6cd3bcc) >_Uarm_dwarf_search_unwind_table: IP b6cd3bcc inside range b6c12000-b6d4c000, but no explicit unwind info found >put_rs_cache: unmasking signals/interrupts and releasing lock >_Uarm_dwarf_step: returning -10 >_Uarm_step: dwarf_step()=-10 This patch passes map->start as segbase to dwarf_find_debug_frame(), so di will be initialized correctly. In addition, dso and executable are different when setting segbase. This patch first check whether the elf is executable, and pass segbase only for shared object. Signed-off-by: Wang Nan <wangnan0@huawei.com> --- tools/perf/tests/dwarf-unwind.c | 36 ++++++++++++++++++++++++++++++++++-- tools/perf/util/unwind-libunwind.c | 28 +++++++++++++++++++++++++++- 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/tools/perf/tests/dwarf-unwind.c b/tools/perf/tests/dwarf-unwind.c index ab28cca..0bf06be 100644 --- a/tools/perf/tests/dwarf-unwind.c +++ b/tools/perf/tests/dwarf-unwind.c @@ -11,6 +11,9 @@ #include "thread.h" #include "callchain.h" +/* For bsearch. We try to unwind functions in shared object. */ +#include <stdlib.h> + static int mmap_handler(struct perf_tool *tool __maybe_unused, union perf_event *event, struct perf_sample *sample __maybe_unused, @@ -28,7 +31,7 @@ static int init_live_machine(struct machine *machine) mmap_handler, machine, true); } -#define MAX_STACK 6 +#define MAX_STACK 8 static int unwind_entry(struct unwind_entry *entry, void *arg) { @@ -37,6 +40,8 @@ static int unwind_entry(struct unwind_entry *entry, void *arg) static const char *funcs[MAX_STACK] = { "test__arch_unwind_sample", "unwind_thread", + "compare", + "bsearch", "krava_3", "krava_2", "krava_1", @@ -88,10 +93,37 @@ static int unwind_thread(struct thread *thread) return err; } +static int global_unwind_retval = -INT_MAX; + +__attribute__ ((noinline)) +static int compare(void *p1, void *p2) +{ + /* Any possible value should be 'thread' */ + struct thread *thread = *(struct thread **)p1; + + if (global_unwind_retval == -INT_MAX) + global_unwind_retval = unwind_thread(thread); + + return p1 - p2; +} + __attribute__ ((noinline)) static int krava_3(struct thread *thread) { - return unwind_thread(thread); + struct thread *array[2] = {thread, thread}; + void *fp = &bsearch; + /* + * make _bsearch a volatile function pointer to + * prevent potential optimization, which may expand + * bsearch and call compare directly from this function, + * instead of libc shared object. + */ + void *(*volatile _bsearch)(void *, void *, size_t, + size_t, int (*)(void *, void *)); + + _bsearch = fp; + _bsearch(array, &thread, 2, sizeof(struct thread **), compare); + return global_unwind_retval; } __attribute__ ((noinline)) diff --git a/tools/perf/util/unwind-libunwind.c b/tools/perf/util/unwind-libunwind.c index 371219a..6edf535 100644 --- a/tools/perf/util/unwind-libunwind.c +++ b/tools/perf/util/unwind-libunwind.c @@ -185,6 +185,28 @@ static u64 elf_section_offset(int fd, const char *name) return offset; } +#ifndef NO_LIBUNWIND_DEBUG_FRAME +static int elf_is_exec(int fd, const char *name) +{ + Elf *elf; + GElf_Ehdr ehdr; + int retval = 0; + + elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); + if (elf == NULL) + return 0; + if (gelf_getehdr(elf, &ehdr) == NULL) + goto out; + + retval = (ehdr.e_type == ET_EXEC); + +out: + elf_end(elf); + pr_debug("unwind: elf_is_exec(%s): %d\n", name, retval); + return retval; +} +#endif + struct table_entry { u32 start_ip_offset; u32 fde_offset; @@ -322,8 +344,12 @@ find_proc_info(unw_addr_space_t as, unw_word_t ip, unw_proc_info_t *pi, #ifndef NO_LIBUNWIND_DEBUG_FRAME /* Check the .debug_frame section for unwinding info */ if (!read_unwind_spec_debug_frame(map->dso, ui->machine, &segbase)) { + int fd = dso__data_fd(map->dso, ui->machine); + int is_exec = elf_is_exec(fd, map->dso->name); + unw_word_t base = is_exec ? 0 : map->start; + memset(&di, 0, sizeof(di)); - if (dwarf_find_debug_frame(0, &di, ip, 0, map->dso->name, + if (dwarf_find_debug_frame(0, &di, ip, base, map->dso->name, map->start, map->end)) return dwarf_search_unwind_table(as, ip, &di, pi, need_unwind_info, arg); -- 1.8.4 ^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH v2] perf: fix dwarf unwind using libunwind. 2015-01-14 2:36 ` [PATCH v2] " Wang Nan @ 2015-01-14 11:57 ` Jiri Olsa 2015-01-14 12:50 ` Arnaldo Carvalho de Melo 2015-01-17 10:12 ` [tip:perf/urgent] perf test: Fix " tip-bot for Wang Nan 1 sibling, 1 reply; 8+ messages in thread From: Jiri Olsa @ 2015-01-14 11:57 UTC (permalink / raw) To: Wang Nan Cc: jolsa, linux-kernel, lizefan, namhyung, a.p.zijlstra, paulus, mingo, acme, masami.hiramatsu.pt On Wed, Jan 14, 2015 at 10:36:47AM +0800, Wang Nan wrote: > Perf tool fails to unwind user stack if the event raises in a shared SNIP > + > __attribute__ ((noinline)) > static int krava_3(struct thread *thread) > { > - return unwind_thread(thread); > + struct thread *array[2] = {thread, thread}; > + void *fp = &bsearch; > + /* > + * make _bsearch a volatile function pointer to > + * prevent potential optimization, which may expand > + * bsearch and call compare directly from this function, > + * instead of libc shared object. > + */ > + void *(*volatile _bsearch)(void *, void *, size_t, > + size_t, int (*)(void *, void *)); > + > + _bsearch = fp; > + _bsearch(array, &thread, 2, sizeof(struct thread **), compare); > + return global_unwind_retval; > } ah, I've got confused with the NO_LIBUNWIND_DEBUG_FRAME name and got the impression that we could use it in the tests/make as another compile option test.. but your change is even better ;-) thanks for updating this test Acked-by: Jiri Olsa <jolsa@kernel.org> thanks, jirka ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v2] perf: fix dwarf unwind using libunwind. 2015-01-14 11:57 ` Jiri Olsa @ 2015-01-14 12:50 ` Arnaldo Carvalho de Melo 0 siblings, 0 replies; 8+ messages in thread From: Arnaldo Carvalho de Melo @ 2015-01-14 12:50 UTC (permalink / raw) To: Jiri Olsa Cc: Wang Nan, jolsa, linux-kernel, lizefan, namhyung, a.p.zijlstra, paulus, mingo, masami.hiramatsu.pt Em Wed, Jan 14, 2015 at 12:57:49PM +0100, Jiri Olsa escreveu: > On Wed, Jan 14, 2015 at 10:36:47AM +0800, Wang Nan wrote: > > Perf tool fails to unwind user stack if the event raises in a shared > > SNIP > > > + > > __attribute__ ((noinline)) > > static int krava_3(struct thread *thread) > > { > > - return unwind_thread(thread); > > + struct thread *array[2] = {thread, thread}; > > + void *fp = &bsearch; > > + /* > > + * make _bsearch a volatile function pointer to > > + * prevent potential optimization, which may expand > > + * bsearch and call compare directly from this function, > > + * instead of libc shared object. > > + */ > > + void *(*volatile _bsearch)(void *, void *, size_t, > > + size_t, int (*)(void *, void *)); > > + > > + _bsearch = fp; > > + _bsearch(array, &thread, 2, sizeof(struct thread **), compare); > > + return global_unwind_retval; > > } > > ah, I've got confused with the NO_LIBUNWIND_DEBUG_FRAME name > and got the impression that we could use it in the tests/make > as another compile option test.. > > but your change is even better ;-) thanks for updating this test > > > Acked-by: Jiri Olsa <jolsa@kernel.org> Thanks, applied to perf/urgent. - Arnaldo ^ permalink raw reply [flat|nested] 8+ messages in thread
* [tip:perf/urgent] perf test: Fix dwarf unwind using libunwind. 2015-01-14 2:36 ` [PATCH v2] " Wang Nan 2015-01-14 11:57 ` Jiri Olsa @ 2015-01-17 10:12 ` tip-bot for Wang Nan 1 sibling, 0 replies; 8+ messages in thread From: tip-bot for Wang Nan @ 2015-01-17 10:12 UTC (permalink / raw) To: linux-tip-commits Cc: mingo, namhyung, lizefan, masami.hiramatsu.pt, wangnan0, hpa, mingo, linux-kernel, acme, a.p.zijlstra, jolsa, tglx, paulus Commit-ID: b93b0967826a4a00297dde1cbbda8df673c1689e Gitweb: http://git.kernel.org/tip/b93b0967826a4a00297dde1cbbda8df673c1689e Author: Wang Nan <wangnan0@huawei.com> AuthorDate: Wed, 14 Jan 2015 10:36:47 +0800 Committer: Arnaldo Carvalho de Melo <acme@redhat.com> CommitDate: Fri, 16 Jan 2015 17:49:29 -0300 perf test: Fix dwarf unwind using libunwind. Perf tool fails to unwind user stack if the event raises in a shared object. This patch improves tests/dwarf-unwind.c to demonstrate the problem by utilizing commonly used glibc function "bsearch". If perf is not statically linked, the testcase will try to unwind a mixed call trace. By debugging libunwind I found that there is a bug in unwind-libunwind: it always passes 0 as segbase to libunwind, cause libunwind unable to locate debug_frame entry fir first level ip address (I add some more debugging output into libunwind to make things clear): >_Uarm_dwarf_find_debug_frame: start_ip = 10be98, end_ip = 10c2a4 >_Uarm_dwarf_find_debug_frame: found debug_frame table `/lib/libc-2.18.so': segbase=0x0, len=7, gp=0x0, table_data=0x449388 >_Uarm_dwarf_search_unwind_table: call lookup:ip = b6cd3bcc, segbase = 0, rel_ip = b6cd3bcc >lookup: e->start_ip_offset = bcf18 (rel_ip = b6cd3bcc) >lookup: e->start_ip_offset = 6d314 (rel_ip = b6cd3bcc) >lookup: e->start_ip_offset = 33d0c (rel_ip = b6cd3bcc) ... >lookup: e->start_ip_offset = 15d0c (rel_ip = b6cd3bcc) >lookup: e->start_ip_offset = 15c40 (rel_ip = b6cd3bcc) >_Uarm_dwarf_search_unwind_table: IP b6cd3bcc inside range b6c12000-b6d4c000, but no explicit unwind info found >put_rs_cache: unmasking signals/interrupts and releasing lock >_Uarm_dwarf_step: returning -10 >_Uarm_step: dwarf_step()=-10 This patch passes map->start as segbase to dwarf_find_debug_frame(), so di will be initialized correctly. In addition, dso and executable are different when setting segbase. This patch first check whether the elf is executable, and pass segbase only for shared object. Signed-off-by: Wang Nan <wangnan0@huawei.com> Acked-by: Jiri Olsa <jolsa@kernel.org> Cc: Ingo Molnar <mingo@redhat.com> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Li Zefan <lizefan@huawei.com> Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Paul Mackerras <paulus@samba.org> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Link: http://lkml.kernel.org/r/1421203007-75799-1-git-send-email-wangnan0@huawei.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com> --- tools/perf/tests/dwarf-unwind.c | 36 ++++++++++++++++++++++++++++++++++-- tools/perf/util/unwind-libunwind.c | 28 +++++++++++++++++++++++++++- 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/tools/perf/tests/dwarf-unwind.c b/tools/perf/tests/dwarf-unwind.c index ab28cca..0bf06be 100644 --- a/tools/perf/tests/dwarf-unwind.c +++ b/tools/perf/tests/dwarf-unwind.c @@ -11,6 +11,9 @@ #include "thread.h" #include "callchain.h" +/* For bsearch. We try to unwind functions in shared object. */ +#include <stdlib.h> + static int mmap_handler(struct perf_tool *tool __maybe_unused, union perf_event *event, struct perf_sample *sample __maybe_unused, @@ -28,7 +31,7 @@ static int init_live_machine(struct machine *machine) mmap_handler, machine, true); } -#define MAX_STACK 6 +#define MAX_STACK 8 static int unwind_entry(struct unwind_entry *entry, void *arg) { @@ -37,6 +40,8 @@ static int unwind_entry(struct unwind_entry *entry, void *arg) static const char *funcs[MAX_STACK] = { "test__arch_unwind_sample", "unwind_thread", + "compare", + "bsearch", "krava_3", "krava_2", "krava_1", @@ -88,10 +93,37 @@ static int unwind_thread(struct thread *thread) return err; } +static int global_unwind_retval = -INT_MAX; + +__attribute__ ((noinline)) +static int compare(void *p1, void *p2) +{ + /* Any possible value should be 'thread' */ + struct thread *thread = *(struct thread **)p1; + + if (global_unwind_retval == -INT_MAX) + global_unwind_retval = unwind_thread(thread); + + return p1 - p2; +} + __attribute__ ((noinline)) static int krava_3(struct thread *thread) { - return unwind_thread(thread); + struct thread *array[2] = {thread, thread}; + void *fp = &bsearch; + /* + * make _bsearch a volatile function pointer to + * prevent potential optimization, which may expand + * bsearch and call compare directly from this function, + * instead of libc shared object. + */ + void *(*volatile _bsearch)(void *, void *, size_t, + size_t, int (*)(void *, void *)); + + _bsearch = fp; + _bsearch(array, &thread, 2, sizeof(struct thread **), compare); + return global_unwind_retval; } __attribute__ ((noinline)) diff --git a/tools/perf/util/unwind-libunwind.c b/tools/perf/util/unwind-libunwind.c index 371219a..6edf535 100644 --- a/tools/perf/util/unwind-libunwind.c +++ b/tools/perf/util/unwind-libunwind.c @@ -185,6 +185,28 @@ static u64 elf_section_offset(int fd, const char *name) return offset; } +#ifndef NO_LIBUNWIND_DEBUG_FRAME +static int elf_is_exec(int fd, const char *name) +{ + Elf *elf; + GElf_Ehdr ehdr; + int retval = 0; + + elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); + if (elf == NULL) + return 0; + if (gelf_getehdr(elf, &ehdr) == NULL) + goto out; + + retval = (ehdr.e_type == ET_EXEC); + +out: + elf_end(elf); + pr_debug("unwind: elf_is_exec(%s): %d\n", name, retval); + return retval; +} +#endif + struct table_entry { u32 start_ip_offset; u32 fde_offset; @@ -322,8 +344,12 @@ find_proc_info(unw_addr_space_t as, unw_word_t ip, unw_proc_info_t *pi, #ifndef NO_LIBUNWIND_DEBUG_FRAME /* Check the .debug_frame section for unwinding info */ if (!read_unwind_spec_debug_frame(map->dso, ui->machine, &segbase)) { + int fd = dso__data_fd(map->dso, ui->machine); + int is_exec = elf_is_exec(fd, map->dso->name); + unw_word_t base = is_exec ? 0 : map->start; + memset(&di, 0, sizeof(di)); - if (dwarf_find_debug_frame(0, &di, ip, 0, map->dso->name, + if (dwarf_find_debug_frame(0, &di, ip, base, map->dso->name, map->start, map->end)) return dwarf_search_unwind_table(as, ip, &di, pi, need_unwind_info, arg); ^ permalink raw reply related [flat|nested] 8+ messages in thread
end of thread, other threads:[~2015-01-17 10:13 UTC | newest] Thread overview: 8+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2015-01-13 2:54 [PATCH] perf: fix dwarf unwind using libunwind Wang Nan 2015-01-13 7:10 ` Jiri Olsa 2015-01-13 8:36 ` Wang Nan 2015-01-13 9:19 ` Jiri Olsa 2015-01-14 2:36 ` [PATCH v2] " Wang Nan 2015-01-14 11:57 ` Jiri Olsa 2015-01-14 12:50 ` Arnaldo Carvalho de Melo 2015-01-17 10:12 ` [tip:perf/urgent] perf test: Fix " tip-bot for Wang Nan
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox