[PATCH] perf probe: read DWARF files from the correct CU

Georg Müller posted 1 patch 2 years, 7 months ago
tools/perf/util/dwarf-aux.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
[PATCH] perf probe: read DWARF files from the correct CU
Posted by Georg Müller 2 years, 7 months ago
After switching from dwarf_decl_file() to die_get_decl_file(), it is not
possible to add probes for certain functions of certain binaries:

 $ perf probe -x /usr/lib/systemd/systemd-logind match_unit_removed
 A function DIE doesn't have decl_line. Maybe broken DWARF?
 A function DIE doesn't have decl_line. Maybe broken DWARF?
 Probe point 'match_unit_removed' not found.
    Error: Failed to add events.

The problem is that die_get_decl_file() uses the wrong CU to search for
the file. elfutils commit e1db5cdc9f has some good explanation for this:

    dwarf_decl_file uses dwarf_attr_integrate to get the DW_AT_decl_file
    attribute. This means the attribute might come from a different DIE
    in a different CU. If so, we need to use the CU associated with the
    attribute, not the original DIE, to resolve the file name.

This patch uses the same source of information as elfutils: use attribute
DW_AT_decl_file and use this CU to search for the file.

Fixes: dc9a5d2ccd5c ("perf probe: Fix to get declared file name from clang DWARF5")
Signed-off-by: Georg Müller <georgmueller@gmx.net>
Link: https://lore.kernel.org/r/5a00d5a5-7be7-ef8a-4044-9a16249fff25@gmx.net/
---
 tools/perf/util/dwarf-aux.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/tools/perf/util/dwarf-aux.c b/tools/perf/util/dwarf-aux.c
index b07414409771..137b3ed9897b 100644
--- a/tools/perf/util/dwarf-aux.c
+++ b/tools/perf/util/dwarf-aux.c
@@ -478,8 +478,10 @@ static const char *die_get_file_name(Dwarf_Die *dw_die, int idx)
 {
 	Dwarf_Die cu_die;
 	Dwarf_Files *files;
+	Dwarf_Attribute attr_mem;

-	if (idx < 0 || !dwarf_diecu(dw_die, &cu_die, NULL, NULL) ||
+	if (idx < 0 || !dwarf_attr_integrate(dw_die, DW_AT_decl_file, &attr_mem) ||
+	    !dwarf_cu_die(attr_mem.cu, &cu_die, NULL, NULL, NULL, NULL, NULL, NULL) ||
 	    dwarf_getsrcfiles(&cu_die, &files, NULL) != 0)
 		return NULL;

--
2.41.0
Re: [PATCH] perf probe: read DWARF files from the correct CU
Posted by Arnaldo Carvalho de Melo 2 years, 7 months ago
Em Thu, Jun 15, 2023 at 10:01:37PM +0200, Georg Müller escreveu:
> After switching from dwarf_decl_file() to die_get_decl_file(), it is not
> possible to add probes for certain functions of certain binaries:
> 
>  $ perf probe -x /usr/lib/systemd/systemd-logind match_unit_removed
>  A function DIE doesn't have decl_line. Maybe broken DWARF?
>  A function DIE doesn't have decl_line. Maybe broken DWARF?
>  Probe point 'match_unit_removed' not found.
>     Error: Failed to add events.
> 
> The problem is that die_get_decl_file() uses the wrong CU to search for
> the file. elfutils commit e1db5cdc9f has some good explanation for this:
> 
>     dwarf_decl_file uses dwarf_attr_integrate to get the DW_AT_decl_file
>     attribute. This means the attribute might come from a different DIE
>     in a different CU. If so, we need to use the CU associated with the
>     attribute, not the original DIE, to resolve the file name.
> 
> This patch uses the same source of information as elfutils: use attribute
> DW_AT_decl_file and use this CU to search for the file.

Thanks, applied to the perf-tools branch, that will be submitted for
Linux v6.5.

- Arnaldo
 
> Fixes: dc9a5d2ccd5c ("perf probe: Fix to get declared file name from clang DWARF5")
> Signed-off-by: Georg Müller <georgmueller@gmx.net>
> Link: https://lore.kernel.org/r/5a00d5a5-7be7-ef8a-4044-9a16249fff25@gmx.net/
> ---
>  tools/perf/util/dwarf-aux.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/perf/util/dwarf-aux.c b/tools/perf/util/dwarf-aux.c
> index b07414409771..137b3ed9897b 100644
> --- a/tools/perf/util/dwarf-aux.c
> +++ b/tools/perf/util/dwarf-aux.c
> @@ -478,8 +478,10 @@ static const char *die_get_file_name(Dwarf_Die *dw_die, int idx)
>  {
>  	Dwarf_Die cu_die;
>  	Dwarf_Files *files;
> +	Dwarf_Attribute attr_mem;
> 
> -	if (idx < 0 || !dwarf_diecu(dw_die, &cu_die, NULL, NULL) ||
> +	if (idx < 0 || !dwarf_attr_integrate(dw_die, DW_AT_decl_file, &attr_mem) ||
> +	    !dwarf_cu_die(attr_mem.cu, &cu_die, NULL, NULL, NULL, NULL, NULL, NULL) ||
>  	    dwarf_getsrcfiles(&cu_die, &files, NULL) != 0)
>  		return NULL;
> 
> --
> 2.41.0
> 

-- 

- Arnaldo
Re: [PATCH] perf probe: read DWARF files from the correct CU
Posted by Masami Hiramatsu (Google) 2 years, 7 months ago
On Tue, 11 Jul 2023 09:57:45 -0300
Arnaldo Carvalho de Melo <acme@kernel.org> wrote:

> Em Thu, Jun 15, 2023 at 10:01:37PM +0200, Georg Müller escreveu:
> > After switching from dwarf_decl_file() to die_get_decl_file(), it is not
> > possible to add probes for certain functions of certain binaries:
> > 
> >  $ perf probe -x /usr/lib/systemd/systemd-logind match_unit_removed
> >  A function DIE doesn't have decl_line. Maybe broken DWARF?
> >  A function DIE doesn't have decl_line. Maybe broken DWARF?
> >  Probe point 'match_unit_removed' not found.
> >     Error: Failed to add events.
> > 
> > The problem is that die_get_decl_file() uses the wrong CU to search for
> > the file. elfutils commit e1db5cdc9f has some good explanation for this:
> > 
> >     dwarf_decl_file uses dwarf_attr_integrate to get the DW_AT_decl_file
> >     attribute. This means the attribute might come from a different DIE
> >     in a different CU. If so, we need to use the CU associated with the
> >     attribute, not the original DIE, to resolve the file name.
> > 
> > This patch uses the same source of information as elfutils: use attribute
> > DW_AT_decl_file and use this CU to search for the file.
> 
> Thanks, applied to the perf-tools branch, that will be submitted for
> Linux v6.5.

Thanks Arnaldo and Georg,

It sounds perfect reason why that happened. I didn't expect the case that
the attribute comes from another CU...

Acked-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>

> 
> - Arnaldo
>  
> > Fixes: dc9a5d2ccd5c ("perf probe: Fix to get declared file name from clang DWARF5")
> > Signed-off-by: Georg Müller <georgmueller@gmx.net>
> > Link: https://lore.kernel.org/r/5a00d5a5-7be7-ef8a-4044-9a16249fff25@gmx.net/
> > ---
> >  tools/perf/util/dwarf-aux.c | 4 +++-
> >  1 file changed, 3 insertions(+), 1 deletion(-)
> > 
> > diff --git a/tools/perf/util/dwarf-aux.c b/tools/perf/util/dwarf-aux.c
> > index b07414409771..137b3ed9897b 100644
> > --- a/tools/perf/util/dwarf-aux.c
> > +++ b/tools/perf/util/dwarf-aux.c
> > @@ -478,8 +478,10 @@ static const char *die_get_file_name(Dwarf_Die *dw_die, int idx)
> >  {
> >  	Dwarf_Die cu_die;
> >  	Dwarf_Files *files;
> > +	Dwarf_Attribute attr_mem;
> > 
> > -	if (idx < 0 || !dwarf_diecu(dw_die, &cu_die, NULL, NULL) ||
> > +	if (idx < 0 || !dwarf_attr_integrate(dw_die, DW_AT_decl_file, &attr_mem) ||
> > +	    !dwarf_cu_die(attr_mem.cu, &cu_die, NULL, NULL, NULL, NULL, NULL, NULL) ||
> >  	    dwarf_getsrcfiles(&cu_die, &files, NULL) != 0)
> >  		return NULL;
> > 
> > --
> > 2.41.0
> > 
> 
> -- 
> 
> - Arnaldo


-- 
Masami Hiramatsu (Google) <mhiramat@kernel.org>
Re: [PATCH] perf probe: read DWARF files from the correct CU
Posted by Arnaldo Carvalho de Melo 2 years, 7 months ago
Em Tue, Jul 11, 2023 at 10:20:00PM +0900, Masami Hiramatsu escreveu:
> On Tue, 11 Jul 2023 09:57:45 -0300 Arnaldo Carvalho de Melo <acme@kernel.org> wrote:
> > Em Thu, Jun 15, 2023 at 10:01:37PM +0200, Georg Müller escreveu:
> > > The problem is that die_get_decl_file() uses the wrong CU to search for
> > > the file. elfutils commit e1db5cdc9f has some good explanation for this:
> > > 
> > >     dwarf_decl_file uses dwarf_attr_integrate to get the DW_AT_decl_file
> > >     attribute. This means the attribute might come from a different DIE
> > >     in a different CU. If so, we need to use the CU associated with the
> > >     attribute, not the original DIE, to resolve the file name.
> > > 
> > > This patch uses the same source of information as elfutils: use attribute
> > > DW_AT_decl_file and use this CU to search for the file.
> > 
> > Thanks, applied to the perf-tools branch, that will be submitted for
> > Linux v6.5.
 
> Thanks Arnaldo and Georg,
 
> It sounds perfect reason why that happened. I didn't expect the case that
> the attribute comes from another CU...
 
> Acked-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
 
Thanks, added it to the cset.

- Arnaldo
Re: [PATCH] perf probe: read DWARF files from the correct CU
Posted by Georg Müller 2 years, 7 months ago
Hi,

Am 15.06.23 um 22:01 schrieb Georg Müller:
> After switching from dwarf_decl_file() to die_get_decl_file(), it is not
> possible to add probes for certain functions of certain binaries:
>
>   $ perf probe -x /usr/lib/systemd/systemd-logind match_unit_removed
>   A function DIE doesn't have decl_line. Maybe broken DWARF?
>   A function DIE doesn't have decl_line. Maybe broken DWARF?
>   Probe point 'match_unit_removed' not found.
>      Error: Failed to add events.
>
> The problem is that die_get_decl_file() uses the wrong CU to search for
> the file. elfutils commit e1db5cdc9f has some good explanation for this:
>
>      dwarf_decl_file uses dwarf_attr_integrate to get the DW_AT_decl_file
>      attribute. This means the attribute might come from a different DIE
>      in a different CU. If so, we need to use the CU associated with the
>      attribute, not the original DIE, to resolve the file name.
>
> This patch uses the same source of information as elfutils: use attribute
> DW_AT_decl_file and use this CU to search for the file.
>
> Fixes: dc9a5d2ccd5c ("perf probe: Fix to get declared file name from clang DWARF5")
> Signed-off-by: Georg Müller <georgmueller@gmx.net>
> Link: https://lore.kernel.org/r/5a00d5a5-7be7-ef8a-4044-9a16249fff25@gmx.net/
> ---
>   tools/perf/util/dwarf-aux.c | 4 +++-
>   1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/tools/perf/util/dwarf-aux.c b/tools/perf/util/dwarf-aux.c
> index b07414409771..137b3ed9897b 100644
> --- a/tools/perf/util/dwarf-aux.c
> +++ b/tools/perf/util/dwarf-aux.c
> @@ -478,8 +478,10 @@ static const char *die_get_file_name(Dwarf_Die *dw_die, int idx)
>   {
>   	Dwarf_Die cu_die;
>   	Dwarf_Files *files;
> +	Dwarf_Attribute attr_mem;
>
> -	if (idx < 0 || !dwarf_diecu(dw_die, &cu_die, NULL, NULL) ||
> +	if (idx < 0 || !dwarf_attr_integrate(dw_die, DW_AT_decl_file, &attr_mem) ||
> +	    !dwarf_cu_die(attr_mem.cu, &cu_die, NULL, NULL, NULL, NULL, NULL, NULL) ||
>   	    dwarf_getsrcfiles(&cu_die, &files, NULL) != 0)
>   		return NULL;
>
> --
> 2.41.0
>


Just a question regarding the patch:
Should I also add a testcase to show the issue or is this enough? It can be
reproduced quite simple with two .c files and one .h file, the same test as the
one added in elfutils with commit e1db5cdc9f, compiled with the same three steps
as in the comment of the shell script, but without "-flto":

https://sourceware.org/git/?p=elfutils.git;a=commitdiff;h=e1db5cdc9f230f8de4df1a0f38dca69b283ee57a

Best regards,
Georg