diff mbox

[3/3] perf: parse the .debug_frame section in case .eh_frame is not present

Message ID 1378317854-25965-4-git-send-email-jean.pihet@linaro.org
State Superseded
Headers show

Commit Message

Jean Pihet Sept. 4, 2013, 6:04 p.m. UTC
On ARM the debug info is not present in the .eh_frame sections but
instead in .debug_frame.
Use libunwind to load and parse the debug info.

Signed-off-by: Jean Pihet <jean.pihet@linaro.org>
---
 tools/perf/util/unwind.c | 70 +++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 54 insertions(+), 16 deletions(-)

Comments

Will Deacon Sept. 5, 2013, 12:45 p.m. UTC | #1
Hi Jean,

[adding Michael, since I know he was interested in this]

On Wed, Sep 04, 2013 at 07:04:14PM +0100, Jean Pihet wrote:
> On ARM the debug info is not present in the .eh_frame sections but
> instead in .debug_frame.
> Use libunwind to load and parse the debug info.

How have you tested this? Regardless of whether or not I apply this patch, I
get the same (broken/truncated) callchains for userspace in perf report.

E.g. the following stupid program (built with -O0 -g):

--->8

void bar(void)
{
        int i;
        for (i = 0; i < 1000000; ++i)
                asm volatile("nop" ::: "memory");
}

void foo(void)
{
        bar();
}


int main(void)
{
        foo();
        return 0;
}

8<---

Gives me an incomplete callchain:

# Overhead   Command      Shared Object                           Symbol
# ........  ........  .................  ...............................
#
     0.00%  unwindme  unwindme           [.] bar                        
            |
            --- bar

This is the same with or without your patch.

Will
Jiri Olsa Sept. 5, 2013, 4:30 p.m. UTC | #2
On Wed, Sep 04, 2013 at 08:04:14PM +0200, Jean Pihet wrote:
> On ARM the debug info is not present in the .eh_frame sections but
> instead in .debug_frame.
> Use libunwind to load and parse the debug info.

hum, cannot make final link:

$ make LIBUNWIND_DIR=/opt/libunwind/
    CHK -fstack-protector-all
    CHK -Wstack-protector
    CHK -Wvolatile-register-var
    CHK -D_FORTIFY_SOURCE=2
    CHK bionic
    CHK libelf
    CHK libdw
    CHK -DLIBELF_MMAP
    CHK -DLIBELF_MMAP
    CHK libunwind
    CHK libaudit

...

make[1]: `liblk.a' is up to date.
    SUBDIR /home/jolsa/linux-perf/tools/lib/traceevent/
    LINK perf
libperf.a(unwind.o): In function `find_proc_info':
/home/jolsa/linux-perf/tools/perf/util/unwind.c:339: undefined reference to `_Ux86_64_dwarf_find_debug_frame'
collect2: ld returned 1 exit status
make: *** [perf] Error 1


I'm using the latest code from git://git.sv.gnu.org/libunwind.git

Looks like dwarf_find_debug_frame is not exported, although
it looks like it is based on what I see in libunwind sources ;-)

What did I miss?

Also few typo comments below..

thanks,
jirka

> 
> Signed-off-by: Jean Pihet <jean.pihet@linaro.org>
> ---
>  tools/perf/util/unwind.c | 70 +++++++++++++++++++++++++++++++++++++-----------
>  1 file changed, 54 insertions(+), 16 deletions(-)
> 
> diff --git a/tools/perf/util/unwind.c b/tools/perf/util/unwind.c
> index 958723b..5353b32 100644
> --- a/tools/perf/util/unwind.c
> +++ b/tools/perf/util/unwind.c
> @@ -39,6 +39,14 @@ UNW_OBJ(dwarf_search_unwind_table) (unw_addr_space_t as,
>  
>  #define dwarf_search_unwind_table UNW_OBJ(dwarf_search_unwind_table)
>  
> +extern int
> +UNW_OBJ(dwarf_find_debug_frame) (int found, unw_dyn_info_t *di_debug,
> +								 unw_word_t ip, unw_word_t segbase,
> +								 const char *obj_name, unw_word_t start,
> +								 unw_word_t end);
> +
> +#define dwarf_find_debug_frame UNW_OBJ(dwarf_find_debug_frame)
> +
>  #define DW_EH_PE_FORMAT_MASK	0x0f	/* format of the encoded value */
>  #define DW_EH_PE_APPL_MASK	0x70	/* how the value is to be applied */
>  
> @@ -245,8 +253,9 @@ static int unwind_spec_ehframe(struct dso *dso, struct machine *machine,
>  	return 0;
>  }
>  
> -static int read_unwind_spec(struct dso *dso, struct machine *machine,
> -			    u64 *table_data, u64 *segbase, u64 *fde_count)
> +static int read_unwind_spec_eh_frame(struct dso *dso, struct machine *machine,
> +				     u64 *table_data, u64 *segbase,
> +				     u64 *fde_count)
>  {
>  	int ret = -EINVAL, fd;
>  	u64 offset;
> @@ -255,6 +264,7 @@ static int read_unwind_spec(struct dso *dso, struct machine *machine,
>  	if (fd < 0)
>  		return -EINVAL;
>  
> +	/* Check the .eh_frame section for unwinding info */
>  	offset = elf_section_offset(fd, ".eh_frame_hdr");
>  	close(fd);
>  
> @@ -263,10 +273,27 @@ static int read_unwind_spec(struct dso *dso, struct machine *machine,
>  					  table_data, segbase,
>  					  fde_count);
>  
> -	/* TODO .debug_frame check if eh_frame_hdr fails */
>  	return ret;
>  }
>  
> +static int read_unwind_spec_debug_frame(struct dso *dso,
> +										struct machine *machine, u64 *offset)
> +{

some strange formatting issue ^^^ ;-)


> +	int fd = dso__data_fd(dso, machine);
> +
> +	if (fd < 0)
> +		return -EINVAL;
> +
> +	/* Check the .debug_frame section for unwinding info */
> +	*offset = elf_section_offset(fd, ".debug_frame");
> +	close(fd);
> +
> +	if (*offset)
> +		return 0;
> +
> +	return -EINVAL;
> +}
> +
>  static struct map *find_map(unw_word_t ip, struct unwind_info *ui)
>  {
>  	struct addr_location al;
> @@ -291,20 +318,31 @@ find_proc_info(unw_addr_space_t as, unw_word_t ip, unw_proc_info_t *pi,
>  
>  	pr_debug("unwind: find_proc_info dso %s\n", map->dso->name);
>  
> -	if (read_unwind_spec(map->dso, ui->machine,
> -			     &table_data, &segbase, &fde_count))
> -		return -EINVAL;
> +	/* Check the .eh_frame section for unwinding info */
> +	if (!read_unwind_spec_eh_frame(map->dso, ui->machine,
> +								   &table_data, &segbase, &fde_count)) {

ditto ^^^

> +		memset(&di, 0, sizeof(di));
> +		di.format   = UNW_INFO_FORMAT_REMOTE_TABLE;
> +		di.start_ip = map->start;
> +		di.end_ip   = map->end;
> +		di.u.rti.segbase    = map->start + segbase;
> +		di.u.rti.table_data = map->start + table_data;
> +		di.u.rti.table_len  = fde_count * sizeof(struct table_entry)
> +							  / sizeof(unw_word_t);
> +	    return dwarf_search_unwind_table(as, ip, &di, pi,
> +										 need_unwind_info, arg);

ditto ^^^

> +	}
> +
> +	/* Check the .debug_frame section for unwinding info */
> +	if (!read_unwind_spec_debug_frame(map->dso, ui->machine, &segbase)) {
> +		memset(&di, 0, sizeof(di));
> +		dwarf_find_debug_frame(0, &di, ip, 0, map->dso->name,
> +							   map->start, map->end);
> +		return dwarf_search_unwind_table(as, ip, &di, pi,
> +										 need_unwind_info, arg);

ditto ^^^

> +	}
>  
> -	memset(&di, 0, sizeof(di));
> -	di.format   = UNW_INFO_FORMAT_REMOTE_TABLE;
> -	di.start_ip = map->start;
> -	di.end_ip   = map->end;
> -	di.u.rti.segbase    = map->start + segbase;
> -	di.u.rti.table_data = map->start + table_data;
> -	di.u.rti.table_len  = fde_count * sizeof(struct table_entry)
> -			      / sizeof(unw_word_t);
> -	return dwarf_search_unwind_table(as, ip, &di, pi,
> -					 need_unwind_info, arg);
> +	return -EINVAL;
>  }
>  
>  static int access_fpreg(unw_addr_space_t __maybe_unused as,
> -- 
> 1.7.11.7
>
Jean Pihet Sept. 5, 2013, 4:49 p.m. UTC | #3
Hi Jiri,

On 5 September 2013 18:30, Jiri Olsa <jolsa@redhat.com> wrote:
> On Wed, Sep 04, 2013 at 08:04:14PM +0200, Jean Pihet wrote:
>> On ARM the debug info is not present in the .eh_frame sections but
>> instead in .debug_frame.
>> Use libunwind to load and parse the debug info.
>
> hum, cannot make final link:
>
> $ make LIBUNWIND_DIR=/opt/libunwind/
>     CHK -fstack-protector-all
>     CHK -Wstack-protector
>     CHK -Wvolatile-register-var
>     CHK -D_FORTIFY_SOURCE=2
>     CHK bionic
>     CHK libelf
>     CHK libdw
>     CHK -DLIBELF_MMAP
>     CHK -DLIBELF_MMAP
>     CHK libunwind
>     CHK libaudit
>
> ...
>
> make[1]: `liblk.a' is up to date.
>     SUBDIR /home/jolsa/linux-perf/tools/lib/traceevent/
>     LINK perf
> libperf.a(unwind.o): In function `find_proc_info':
> /home/jolsa/linux-perf/tools/perf/util/unwind.c:339: undefined reference to `_Ux86_64_dwarf_find_debug_frame'
> collect2: ld returned 1 exit status
> make: *** [perf] Error 1
>
>
> I'm using the latest code from git://git.sv.gnu.org/libunwind.git
>
> Looks like dwarf_find_debug_frame is not exported, although
> it looks like it is based on what I see in libunwind sources ;-)
>
> What did I miss?
Weird, I do not have the error on x86_64. I am investigating this and
will come back to you asap.

>
> Also few typo comments below..
>
> thanks,
> jirka
>
>>
>> Signed-off-by: Jean Pihet <jean.pihet@linaro.org>
>> ---
>>  tools/perf/util/unwind.c | 70 +++++++++++++++++++++++++++++++++++++-----------
>>  1 file changed, 54 insertions(+), 16 deletions(-)
>>
>> diff --git a/tools/perf/util/unwind.c b/tools/perf/util/unwind.c
>> index 958723b..5353b32 100644
>> --- a/tools/perf/util/unwind.c
>> +++ b/tools/perf/util/unwind.c
>> @@ -39,6 +39,14 @@ UNW_OBJ(dwarf_search_unwind_table) (unw_addr_space_t as,
>>
>>  #define dwarf_search_unwind_table UNW_OBJ(dwarf_search_unwind_table)
>>
>> +extern int
>> +UNW_OBJ(dwarf_find_debug_frame) (int found, unw_dyn_info_t *di_debug,
>> +                                                              unw_word_t ip, unw_word_t segbase,
>> +                                                              const char *obj_name, unw_word_t start,
>> +                                                              unw_word_t end);
>> +
>> +#define dwarf_find_debug_frame UNW_OBJ(dwarf_find_debug_frame)
>> +
>>  #define DW_EH_PE_FORMAT_MASK 0x0f    /* format of the encoded value */
>>  #define DW_EH_PE_APPL_MASK   0x70    /* how the value is to be applied */
>>
>> @@ -245,8 +253,9 @@ static int unwind_spec_ehframe(struct dso *dso, struct machine *machine,
>>       return 0;
>>  }
>>
>> -static int read_unwind_spec(struct dso *dso, struct machine *machine,
>> -                         u64 *table_data, u64 *segbase, u64 *fde_count)
>> +static int read_unwind_spec_eh_frame(struct dso *dso, struct machine *machine,
>> +                                  u64 *table_data, u64 *segbase,
>> +                                  u64 *fde_count)
>>  {
>>       int ret = -EINVAL, fd;
>>       u64 offset;
>> @@ -255,6 +264,7 @@ static int read_unwind_spec(struct dso *dso, struct machine *machine,
>>       if (fd < 0)
>>               return -EINVAL;
>>
>> +     /* Check the .eh_frame section for unwinding info */
>>       offset = elf_section_offset(fd, ".eh_frame_hdr");
>>       close(fd);
>>
>> @@ -263,10 +273,27 @@ static int read_unwind_spec(struct dso *dso, struct machine *machine,
>>                                         table_data, segbase,
>>                                         fde_count);
>>
>> -     /* TODO .debug_frame check if eh_frame_hdr fails */
>>       return ret;
>>  }
>>
>> +static int read_unwind_spec_debug_frame(struct dso *dso,
>> +                                                                             struct machine *machine, u64 *offset)
>> +{
>
> some strange formatting issue ^^^ ;-)
I am using 4-spaces tabs. scripts/checkpatch.pl reported it to me in
the form of too long lines.
Should I change the code to 8-spaces tabs?

Thx!
Jean

>
>
>> +     int fd = dso__data_fd(dso, machine);
>> +
>> +     if (fd < 0)
>> +             return -EINVAL;
>> +
>> +     /* Check the .debug_frame section for unwinding info */
>> +     *offset = elf_section_offset(fd, ".debug_frame");
>> +     close(fd);
>> +
>> +     if (*offset)
>> +             return 0;
>> +
>> +     return -EINVAL;
>> +}
>> +
>>  static struct map *find_map(unw_word_t ip, struct unwind_info *ui)
>>  {
>>       struct addr_location al;
>> @@ -291,20 +318,31 @@ find_proc_info(unw_addr_space_t as, unw_word_t ip, unw_proc_info_t *pi,
>>
>>       pr_debug("unwind: find_proc_info dso %s\n", map->dso->name);
>>
>> -     if (read_unwind_spec(map->dso, ui->machine,
>> -                          &table_data, &segbase, &fde_count))
>> -             return -EINVAL;
>> +     /* Check the .eh_frame section for unwinding info */
>> +     if (!read_unwind_spec_eh_frame(map->dso, ui->machine,
>> +                                                                &table_data, &segbase, &fde_count)) {
>
> ditto ^^^
>
>> +             memset(&di, 0, sizeof(di));
>> +             di.format   = UNW_INFO_FORMAT_REMOTE_TABLE;
>> +             di.start_ip = map->start;
>> +             di.end_ip   = map->end;
>> +             di.u.rti.segbase    = map->start + segbase;
>> +             di.u.rti.table_data = map->start + table_data;
>> +             di.u.rti.table_len  = fde_count * sizeof(struct table_entry)
>> +                                                       / sizeof(unw_word_t);
>> +         return dwarf_search_unwind_table(as, ip, &di, pi,
>> +                                                                              need_unwind_info, arg);
>
> ditto ^^^
>
>> +     }
>> +
>> +     /* Check the .debug_frame section for unwinding info */
>> +     if (!read_unwind_spec_debug_frame(map->dso, ui->machine, &segbase)) {
>> +             memset(&di, 0, sizeof(di));
>> +             dwarf_find_debug_frame(0, &di, ip, 0, map->dso->name,
>> +                                                        map->start, map->end);
>> +             return dwarf_search_unwind_table(as, ip, &di, pi,
>> +                                                                              need_unwind_info, arg);
>
> ditto ^^^
>
>> +     }
>>
>> -     memset(&di, 0, sizeof(di));
>> -     di.format   = UNW_INFO_FORMAT_REMOTE_TABLE;
>> -     di.start_ip = map->start;
>> -     di.end_ip   = map->end;
>> -     di.u.rti.segbase    = map->start + segbase;
>> -     di.u.rti.table_data = map->start + table_data;
>> -     di.u.rti.table_len  = fde_count * sizeof(struct table_entry)
>> -                           / sizeof(unw_word_t);
>> -     return dwarf_search_unwind_table(as, ip, &di, pi,
>> -                                      need_unwind_info, arg);
>> +     return -EINVAL;
>>  }
>>
>>  static int access_fpreg(unw_addr_space_t __maybe_unused as,
>> --
>> 1.7.11.7
>>
Jean Pihet Sept. 6, 2013, 9:31 a.m. UTC | #4
Hi Jiri,

On 5 September 2013 18:30, Jiri Olsa <jolsa@redhat.com> wrote:
> On Wed, Sep 04, 2013 at 08:04:14PM +0200, Jean Pihet wrote:
>> On ARM the debug info is not present in the .eh_frame sections but
>> instead in .debug_frame.
>> Use libunwind to load and parse the debug info.
>
> hum, cannot make final link:
>
> $ make LIBUNWIND_DIR=/opt/libunwind/
>     CHK -fstack-protector-all
>     CHK -Wstack-protector
>     CHK -Wvolatile-register-var
>     CHK -D_FORTIFY_SOURCE=2
>     CHK bionic
>     CHK libelf
>     CHK libdw
>     CHK -DLIBELF_MMAP
>     CHK -DLIBELF_MMAP
>     CHK libunwind
>     CHK libaudit
>
> ...
>
> make[1]: `liblk.a' is up to date.
>     SUBDIR /home/jolsa/linux-perf/tools/lib/traceevent/
>     LINK perf
> libperf.a(unwind.o): In function `find_proc_info':
> /home/jolsa/linux-perf/tools/perf/util/unwind.c:339: undefined reference to `_Ux86_64_dwarf_find_debug_frame'
> collect2: ld returned 1 exit status
> make: *** [perf] Error 1
>
>
> I'm using the latest code from git://git.sv.gnu.org/libunwind.git
>
> Looks like dwarf_find_debug_frame is not exported, although
> it looks like it is based on what I see in libunwind sources ;-)
>
> What did I miss?
libunwind needs to be configured with --enable-debug-frame for the
debug_frame code to be included in the lib.
On ARM the flag is always set while it isn't on x86. Here is the
culprit below (lines from libunwind configure).

Should that be changed in configure along with the changes in tools/perf?


{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether to load
.debug_frame  sections" >&5
$as_echo_n "checking whether to load .debug_frame sections... " >&6; }
# Check whether --enable-debug_frame was given.
if test "${enable_debug_frame+set}" = set; then :
  enableval=$enable_debug_frame;
else

case "${target_arch}" in
  (arm) enable_debug_frame=yes;;
  (*)   enable_debug_frame=no;;
esac
fi

if test x$enable_debug_frame = xyes; then
$as_echo "#define CONFIG_DEBUG_FRAME /**/" >>confdefs.h
fi


Regards,
Jean

>
> Also few typo comments below..
>
> thanks,
> jirka
>
>>
>> Signed-off-by: Jean Pihet <jean.pihet@linaro.org>
>> ---
>>  tools/perf/util/unwind.c | 70 +++++++++++++++++++++++++++++++++++++-----------
>>  1 file changed, 54 insertions(+), 16 deletions(-)
>>
>> diff --git a/tools/perf/util/unwind.c b/tools/perf/util/unwind.c
>> index 958723b..5353b32 100644
>> --- a/tools/perf/util/unwind.c
>> +++ b/tools/perf/util/unwind.c
>> @@ -39,6 +39,14 @@ UNW_OBJ(dwarf_search_unwind_table) (unw_addr_space_t as,
>>
>>  #define dwarf_search_unwind_table UNW_OBJ(dwarf_search_unwind_table)
>>
>> +extern int
>> +UNW_OBJ(dwarf_find_debug_frame) (int found, unw_dyn_info_t *di_debug,
>> +                                                              unw_word_t ip, unw_word_t segbase,
>> +                                                              const char *obj_name, unw_word_t start,
>> +                                                              unw_word_t end);
>> +
>> +#define dwarf_find_debug_frame UNW_OBJ(dwarf_find_debug_frame)
>> +
>>  #define DW_EH_PE_FORMAT_MASK 0x0f    /* format of the encoded value */
>>  #define DW_EH_PE_APPL_MASK   0x70    /* how the value is to be applied */
>>
>> @@ -245,8 +253,9 @@ static int unwind_spec_ehframe(struct dso *dso, struct machine *machine,
>>       return 0;
>>  }
>>
>> -static int read_unwind_spec(struct dso *dso, struct machine *machine,
>> -                         u64 *table_data, u64 *segbase, u64 *fde_count)
>> +static int read_unwind_spec_eh_frame(struct dso *dso, struct machine *machine,
>> +                                  u64 *table_data, u64 *segbase,
>> +                                  u64 *fde_count)
>>  {
>>       int ret = -EINVAL, fd;
>>       u64 offset;
>> @@ -255,6 +264,7 @@ static int read_unwind_spec(struct dso *dso, struct machine *machine,
>>       if (fd < 0)
>>               return -EINVAL;
>>
>> +     /* Check the .eh_frame section for unwinding info */
>>       offset = elf_section_offset(fd, ".eh_frame_hdr");
>>       close(fd);
>>
>> @@ -263,10 +273,27 @@ static int read_unwind_spec(struct dso *dso, struct machine *machine,
>>                                         table_data, segbase,
>>                                         fde_count);
>>
>> -     /* TODO .debug_frame check if eh_frame_hdr fails */
>>       return ret;
>>  }
>>
>> +static int read_unwind_spec_debug_frame(struct dso *dso,
>> +                                                                             struct machine *machine, u64 *offset)
>> +{
>
> some strange formatting issue ^^^ ;-)
>
>
>> +     int fd = dso__data_fd(dso, machine);
>> +
>> +     if (fd < 0)
>> +             return -EINVAL;
>> +
>> +     /* Check the .debug_frame section for unwinding info */
>> +     *offset = elf_section_offset(fd, ".debug_frame");
>> +     close(fd);
>> +
>> +     if (*offset)
>> +             return 0;
>> +
>> +     return -EINVAL;
>> +}
>> +
>>  static struct map *find_map(unw_word_t ip, struct unwind_info *ui)
>>  {
>>       struct addr_location al;
>> @@ -291,20 +318,31 @@ find_proc_info(unw_addr_space_t as, unw_word_t ip, unw_proc_info_t *pi,
>>
>>       pr_debug("unwind: find_proc_info dso %s\n", map->dso->name);
>>
>> -     if (read_unwind_spec(map->dso, ui->machine,
>> -                          &table_data, &segbase, &fde_count))
>> -             return -EINVAL;
>> +     /* Check the .eh_frame section for unwinding info */
>> +     if (!read_unwind_spec_eh_frame(map->dso, ui->machine,
>> +                                                                &table_data, &segbase, &fde_count)) {
>
> ditto ^^^
>
>> +             memset(&di, 0, sizeof(di));
>> +             di.format   = UNW_INFO_FORMAT_REMOTE_TABLE;
>> +             di.start_ip = map->start;
>> +             di.end_ip   = map->end;
>> +             di.u.rti.segbase    = map->start + segbase;
>> +             di.u.rti.table_data = map->start + table_data;
>> +             di.u.rti.table_len  = fde_count * sizeof(struct table_entry)
>> +                                                       / sizeof(unw_word_t);
>> +         return dwarf_search_unwind_table(as, ip, &di, pi,
>> +                                                                              need_unwind_info, arg);
>
> ditto ^^^
>
>> +     }
>> +
>> +     /* Check the .debug_frame section for unwinding info */
>> +     if (!read_unwind_spec_debug_frame(map->dso, ui->machine, &segbase)) {
>> +             memset(&di, 0, sizeof(di));
>> +             dwarf_find_debug_frame(0, &di, ip, 0, map->dso->name,
>> +                                                        map->start, map->end);
>> +             return dwarf_search_unwind_table(as, ip, &di, pi,
>> +                                                                              need_unwind_info, arg);
>
> ditto ^^^
>
>> +     }
>>
>> -     memset(&di, 0, sizeof(di));
>> -     di.format   = UNW_INFO_FORMAT_REMOTE_TABLE;
>> -     di.start_ip = map->start;
>> -     di.end_ip   = map->end;
>> -     di.u.rti.segbase    = map->start + segbase;
>> -     di.u.rti.table_data = map->start + table_data;
>> -     di.u.rti.table_len  = fde_count * sizeof(struct table_entry)
>> -                           / sizeof(unw_word_t);
>> -     return dwarf_search_unwind_table(as, ip, &di, pi,
>> -                                      need_unwind_info, arg);
>> +     return -EINVAL;
>>  }
>>
>>  static int access_fpreg(unw_addr_space_t __maybe_unused as,
>> --
>> 1.7.11.7
>>
Jiri Olsa Sept. 6, 2013, 10:17 a.m. UTC | #5
On Thu, Sep 05, 2013 at 06:49:13PM +0200, Jean Pihet wrote:
> Hi Jiri,
> 
> On 5 September 2013 18:30, Jiri Olsa <jolsa@redhat.com> wrote:
> > On Wed, Sep 04, 2013 at 08:04:14PM +0200, Jean Pihet wrote:
> >> On ARM the debug info is not present in the .eh_frame sections but
> >> instead in .debug_frame.
> >> Use libunwind to load and parse the debug info.
> >
> > hum, cannot make final link:
> >
> > $ make LIBUNWIND_DIR=/opt/libunwind/
> >     CHK -fstack-protector-all
> >     CHK -Wstack-protector
> >     CHK -Wvolatile-register-var
> >     CHK -D_FORTIFY_SOURCE=2
> >     CHK bionic
> >     CHK libelf
> >     CHK libdw
> >     CHK -DLIBELF_MMAP
> >     CHK -DLIBELF_MMAP
> >     CHK libunwind
> >     CHK libaudit
> >
> > ...
> >
> > make[1]: `liblk.a' is up to date.
> >     SUBDIR /home/jolsa/linux-perf/tools/lib/traceevent/
> >     LINK perf
> > libperf.a(unwind.o): In function `find_proc_info':
> > /home/jolsa/linux-perf/tools/perf/util/unwind.c:339: undefined reference to `_Ux86_64_dwarf_find_debug_frame'
> > collect2: ld returned 1 exit status
> > make: *** [perf] Error 1
> >
> >
> > I'm using the latest code from git://git.sv.gnu.org/libunwind.git
> >
> > Looks like dwarf_find_debug_frame is not exported, although
> > it looks like it is based on what I see in libunwind sources ;-)
> >
> > What did I miss?
> Weird, I do not have the error on x86_64. I am investigating this and
> will come back to you asap.
> 
> >
> > Also few typo comments below..
> >
> > thanks,
> > jirka
> >
> >>
> >> Signed-off-by: Jean Pihet <jean.pihet@linaro.org>
> >> ---
> >>  tools/perf/util/unwind.c | 70 +++++++++++++++++++++++++++++++++++++-----------
> >>  1 file changed, 54 insertions(+), 16 deletions(-)
> >>
> >> diff --git a/tools/perf/util/unwind.c b/tools/perf/util/unwind.c
> >> index 958723b..5353b32 100644
> >> --- a/tools/perf/util/unwind.c
> >> +++ b/tools/perf/util/unwind.c
> >> @@ -39,6 +39,14 @@ UNW_OBJ(dwarf_search_unwind_table) (unw_addr_space_t as,
> >>
> >>  #define dwarf_search_unwind_table UNW_OBJ(dwarf_search_unwind_table)
> >>
> >> +extern int
> >> +UNW_OBJ(dwarf_find_debug_frame) (int found, unw_dyn_info_t *di_debug,
> >> +                                                              unw_word_t ip, unw_word_t segbase,
> >> +                                                              const char *obj_name, unw_word_t start,
> >> +                                                              unw_word_t end);
> >> +
> >> +#define dwarf_find_debug_frame UNW_OBJ(dwarf_find_debug_frame)
> >> +
> >>  #define DW_EH_PE_FORMAT_MASK 0x0f    /* format of the encoded value */
> >>  #define DW_EH_PE_APPL_MASK   0x70    /* how the value is to be applied */
> >>
> >> @@ -245,8 +253,9 @@ static int unwind_spec_ehframe(struct dso *dso, struct machine *machine,
> >>       return 0;
> >>  }
> >>
> >> -static int read_unwind_spec(struct dso *dso, struct machine *machine,
> >> -                         u64 *table_data, u64 *segbase, u64 *fde_count)
> >> +static int read_unwind_spec_eh_frame(struct dso *dso, struct machine *machine,
> >> +                                  u64 *table_data, u64 *segbase,
> >> +                                  u64 *fde_count)
> >>  {
> >>       int ret = -EINVAL, fd;
> >>       u64 offset;
> >> @@ -255,6 +264,7 @@ static int read_unwind_spec(struct dso *dso, struct machine *machine,
> >>       if (fd < 0)
> >>               return -EINVAL;
> >>
> >> +     /* Check the .eh_frame section for unwinding info */
> >>       offset = elf_section_offset(fd, ".eh_frame_hdr");
> >>       close(fd);
> >>
> >> @@ -263,10 +273,27 @@ static int read_unwind_spec(struct dso *dso, struct machine *machine,
> >>                                         table_data, segbase,
> >>                                         fde_count);
> >>
> >> -     /* TODO .debug_frame check if eh_frame_hdr fails */
> >>       return ret;
> >>  }
> >>
> >> +static int read_unwind_spec_debug_frame(struct dso *dso,
> >> +                                                                             struct machine *machine, u64 *offset)
> >> +{
> >
> > some strange formatting issue ^^^ ;-)
> I am using 4-spaces tabs. scripts/checkpatch.pl reported it to me in
> the form of too long lines.
> Should I change the code to 8-spaces tabs?
> 

right, if I set tabs to 4 space then it's ok
so I guess 8 is globally agreed ;-)

jirka
Jiri Olsa Sept. 6, 2013, 10:17 a.m. UTC | #6
On Fri, Sep 06, 2013 at 11:31:17AM +0200, Jean Pihet wrote:
> Hi Jiri,
> 
> On 5 September 2013 18:30, Jiri Olsa <jolsa@redhat.com> wrote:
> > On Wed, Sep 04, 2013 at 08:04:14PM +0200, Jean Pihet wrote:
> >> On ARM the debug info is not present in the .eh_frame sections but
> >> instead in .debug_frame.
> >> Use libunwind to load and parse the debug info.
> >
> > hum, cannot make final link:
> >
> > $ make LIBUNWIND_DIR=/opt/libunwind/
> >     CHK -fstack-protector-all
> >     CHK -Wstack-protector
> >     CHK -Wvolatile-register-var
> >     CHK -D_FORTIFY_SOURCE=2
> >     CHK bionic
> >     CHK libelf
> >     CHK libdw
> >     CHK -DLIBELF_MMAP
> >     CHK -DLIBELF_MMAP
> >     CHK libunwind
> >     CHK libaudit
> >
> > ...
> >
> > make[1]: `liblk.a' is up to date.
> >     SUBDIR /home/jolsa/linux-perf/tools/lib/traceevent/
> >     LINK perf
> > libperf.a(unwind.o): In function `find_proc_info':
> > /home/jolsa/linux-perf/tools/perf/util/unwind.c:339: undefined reference to `_Ux86_64_dwarf_find_debug_frame'
> > collect2: ld returned 1 exit status
> > make: *** [perf] Error 1
> >
> >
> > I'm using the latest code from git://git.sv.gnu.org/libunwind.git
> >
> > Looks like dwarf_find_debug_frame is not exported, although
> > it looks like it is based on what I see in libunwind sources ;-)
> >
> > What did I miss?
> libunwind needs to be configured with --enable-debug-frame for the
> debug_frame code to be included in the lib.
> On ARM the flag is always set while it isn't on x86. Here is the
> culprit below (lines from libunwind configure).

yay, thats it!

> 
> Should that be changed in configure along with the changes in tools/perf?

I guess it's ok

Other than that tabs misformating the 'perf tool' change looks ok.

I tested the '.eh_frame' code and it's still working. Once
I figure out how to create a x86 binary with .debug_frame
data I'll test the code itself ;-)

If you could think of any automated testcase for this that could
be added under 'tests' that'd be nice (not necessarily)

thanks,
jirka
Jean Pihet Sept. 6, 2013, 2:10 p.m. UTC | #7
On 6 September 2013 12:17, Jiri Olsa <jolsa@redhat.com> wrote:
> On Fri, Sep 06, 2013 at 11:31:17AM +0200, Jean Pihet wrote:
>> Hi Jiri,
>>
>> On 5 September 2013 18:30, Jiri Olsa <jolsa@redhat.com> wrote:
>> > On Wed, Sep 04, 2013 at 08:04:14PM +0200, Jean Pihet wrote:
>> >> On ARM the debug info is not present in the .eh_frame sections but
>> >> instead in .debug_frame.
>> >> Use libunwind to load and parse the debug info.
>> >
>> > hum, cannot make final link:
>> >
>> > $ make LIBUNWIND_DIR=/opt/libunwind/
>> >     CHK -fstack-protector-all
>> >     CHK -Wstack-protector
>> >     CHK -Wvolatile-register-var
>> >     CHK -D_FORTIFY_SOURCE=2
>> >     CHK bionic
>> >     CHK libelf
>> >     CHK libdw
>> >     CHK -DLIBELF_MMAP
>> >     CHK -DLIBELF_MMAP
>> >     CHK libunwind
>> >     CHK libaudit
>> >
>> > ...
>> >
>> > make[1]: `liblk.a' is up to date.
>> >     SUBDIR /home/jolsa/linux-perf/tools/lib/traceevent/
>> >     LINK perf
>> > libperf.a(unwind.o): In function `find_proc_info':
>> > /home/jolsa/linux-perf/tools/perf/util/unwind.c:339: undefined reference to `_Ux86_64_dwarf_find_debug_frame'
>> > collect2: ld returned 1 exit status
>> > make: *** [perf] Error 1
>> >
>> >
>> > I'm using the latest code from git://git.sv.gnu.org/libunwind.git
>> >
>> > Looks like dwarf_find_debug_frame is not exported, although
>> > it looks like it is based on what I see in libunwind sources ;-)
>> >
>> > What did I miss?
>> libunwind needs to be configured with --enable-debug-frame for the
>> debug_frame code to be included in the lib.
>> On ARM the flag is always set while it isn't on x86. Here is the
>> culprit below (lines from libunwind configure).
>
> yay, thats it!
>
>>
>> Should that be changed in configure along with the changes in tools/perf?
>
> I guess it's ok
I wonder how to express the dependency between the perf changes and
the libunwind version and configure flags. Is a note in the commit
descritpion enough?
I fear that the change can break many build systems...

>
> Other than that tabs misformating the 'perf tool' change looks ok.
Ok I will respin the code with the typo and formatting changes.

>
> I tested the '.eh_frame' code and it's still working. Once
> I figure out how to create a x86 binary with .debug_frame
> data I'll test the code itself ;-)
Great!

>
> If you could think of any automated testcase for this that could
> be added under 'tests' that'd be nice (not necessarily)
Ok will look at it. Linaro has automated tests that I need to write as
well so rather do it in perf/tests directly ;-p

>
> thanks,
> jirka

Thanks!
Jean
diff mbox

Patch

diff --git a/tools/perf/util/unwind.c b/tools/perf/util/unwind.c
index 958723b..5353b32 100644
--- a/tools/perf/util/unwind.c
+++ b/tools/perf/util/unwind.c
@@ -39,6 +39,14 @@  UNW_OBJ(dwarf_search_unwind_table) (unw_addr_space_t as,
 
 #define dwarf_search_unwind_table UNW_OBJ(dwarf_search_unwind_table)
 
+extern int
+UNW_OBJ(dwarf_find_debug_frame) (int found, unw_dyn_info_t *di_debug,
+								 unw_word_t ip, unw_word_t segbase,
+								 const char *obj_name, unw_word_t start,
+								 unw_word_t end);
+
+#define dwarf_find_debug_frame UNW_OBJ(dwarf_find_debug_frame)
+
 #define DW_EH_PE_FORMAT_MASK	0x0f	/* format of the encoded value */
 #define DW_EH_PE_APPL_MASK	0x70	/* how the value is to be applied */
 
@@ -245,8 +253,9 @@  static int unwind_spec_ehframe(struct dso *dso, struct machine *machine,
 	return 0;
 }
 
-static int read_unwind_spec(struct dso *dso, struct machine *machine,
-			    u64 *table_data, u64 *segbase, u64 *fde_count)
+static int read_unwind_spec_eh_frame(struct dso *dso, struct machine *machine,
+				     u64 *table_data, u64 *segbase,
+				     u64 *fde_count)
 {
 	int ret = -EINVAL, fd;
 	u64 offset;
@@ -255,6 +264,7 @@  static int read_unwind_spec(struct dso *dso, struct machine *machine,
 	if (fd < 0)
 		return -EINVAL;
 
+	/* Check the .eh_frame section for unwinding info */
 	offset = elf_section_offset(fd, ".eh_frame_hdr");
 	close(fd);
 
@@ -263,10 +273,27 @@  static int read_unwind_spec(struct dso *dso, struct machine *machine,
 					  table_data, segbase,
 					  fde_count);
 
-	/* TODO .debug_frame check if eh_frame_hdr fails */
 	return ret;
 }
 
+static int read_unwind_spec_debug_frame(struct dso *dso,
+										struct machine *machine, u64 *offset)
+{
+	int fd = dso__data_fd(dso, machine);
+
+	if (fd < 0)
+		return -EINVAL;
+
+	/* Check the .debug_frame section for unwinding info */
+	*offset = elf_section_offset(fd, ".debug_frame");
+	close(fd);
+
+	if (*offset)
+		return 0;
+
+	return -EINVAL;
+}
+
 static struct map *find_map(unw_word_t ip, struct unwind_info *ui)
 {
 	struct addr_location al;
@@ -291,20 +318,31 @@  find_proc_info(unw_addr_space_t as, unw_word_t ip, unw_proc_info_t *pi,
 
 	pr_debug("unwind: find_proc_info dso %s\n", map->dso->name);
 
-	if (read_unwind_spec(map->dso, ui->machine,
-			     &table_data, &segbase, &fde_count))
-		return -EINVAL;
+	/* Check the .eh_frame section for unwinding info */
+	if (!read_unwind_spec_eh_frame(map->dso, ui->machine,
+								   &table_data, &segbase, &fde_count)) {
+		memset(&di, 0, sizeof(di));
+		di.format   = UNW_INFO_FORMAT_REMOTE_TABLE;
+		di.start_ip = map->start;
+		di.end_ip   = map->end;
+		di.u.rti.segbase    = map->start + segbase;
+		di.u.rti.table_data = map->start + table_data;
+		di.u.rti.table_len  = fde_count * sizeof(struct table_entry)
+							  / sizeof(unw_word_t);
+	    return dwarf_search_unwind_table(as, ip, &di, pi,
+										 need_unwind_info, arg);
+	}
+
+	/* Check the .debug_frame section for unwinding info */
+	if (!read_unwind_spec_debug_frame(map->dso, ui->machine, &segbase)) {
+		memset(&di, 0, sizeof(di));
+		dwarf_find_debug_frame(0, &di, ip, 0, map->dso->name,
+							   map->start, map->end);
+		return dwarf_search_unwind_table(as, ip, &di, pi,
+										 need_unwind_info, arg);
+	}
 
-	memset(&di, 0, sizeof(di));
-	di.format   = UNW_INFO_FORMAT_REMOTE_TABLE;
-	di.start_ip = map->start;
-	di.end_ip   = map->end;
-	di.u.rti.segbase    = map->start + segbase;
-	di.u.rti.table_data = map->start + table_data;
-	di.u.rti.table_len  = fde_count * sizeof(struct table_entry)
-			      / sizeof(unw_word_t);
-	return dwarf_search_unwind_table(as, ip, &di, pi,
-					 need_unwind_info, arg);
+	return -EINVAL;
 }
 
 static int access_fpreg(unw_addr_space_t __maybe_unused as,