Skip to contentSkip to conversation

perf tools: Random fixes for DWARF unwind

PATCH Applied Namhyung Kim 4 participants 12 messages Dec 12, 2023 – Dec 14, 2023 perf events

lore
Share & tools
lore

Namhyung Kim

author maintainer
#0 lore raw

cover perf tools: Random fixes for DWARF unwind

Hello,

I've found a couple of issues on the unwind code while I'm playing with the JIT-dump code for the CPython. The code assumes normal DSOs mapped from the beginning of the file and aligned to the page size. But it's not true for the JIT-dumped DSOs which are generated for each function.

Depending on the JIT implementation, the code address and accompanied ELF info (like ELF file headers and unwind info) can be overlapped to adjacent (JIT-dumped) DSOs. So it should take more care when it calculates the mapping address for the DSO.

It seems these changes need to go to the stable trees but they are changed a lot since then so I'm not sure.

Thanks,
Namhyung

Namhyung Kim (3):
perf genelf: Set ELF program header addresses properly perf unwind-libdw: Handle JIT-generated DSOs properly perf unwind-libunwind: Fix base address for .eh_frame

 tools/perf/util/genelf.c                 |  6 +++---
 tools/perf/util/unwind-libdw.c           | 21 +++++++++++++++++----
 tools/perf/util/unwind-libunwind-local.c |  2 +-
 3 files changed, 21 insertions(+), 8 deletions(-)
Signature · 1 line
2.43.0.472.g3155946c3a-goog
author maintainer
#1 lore raw

PATCH 1/3 perf genelf: Set ELF program header addresses properly

The text section starts after the ELF headers so PHDR.p_vaddr and others should have the correct addresses.

Fixes babd04386b1d ("perf jit: Include program header in ELF files")
---
 tools/perf/util/genelf.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tools/perf/util/genelf.c +3 -3
index fefc72066c4e..ac17a3cb59dc 100644
@@ -293,9 +293,9 @@ jit_write_elf(int fd, uint64_t load_addr, const char *sym,
 	 */
 	phdr = elf_newphdr(e, 1);
 	phdr[0].p_type = PT_LOAD;
-	phdr[0].p_offset = 0;
-	phdr[0].p_vaddr = 0;
-	phdr[0].p_paddr = 0;
+	phdr[0].p_offset = GEN_ELF_TEXT_OFFSET;
+	phdr[0].p_vaddr = GEN_ELF_TEXT_OFFSET;
+	phdr[0].p_paddr = GEN_ELF_TEXT_OFFSET;
 	phdr[0].p_filesz = csize;
 	phdr[0].p_memsz = csize;
 	phdr[0].p_flags = PF_X | PF_R;
Signature · 1 line
2.43.0.472.g3155946c3a-goog
reviewer Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] perf genelf: Set ELF program header addresses properly

Quoted 6 lines · Namhyung Kim

The text section starts after the ELF headers so PHDR.p_vaddr and others should have the correct addresses.

Fixes babd04386b1d ("perf jit: Include program header in ELF files")

Thanks,
Ian

Quoted 24 lines
---
 tools/perf/util/genelf.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tools/perf/util/genelf.c +3 -4
index fefc72066c4e..ac17a3cb59dc 100644
@@ -293,9 +293,9 @@ jit_write_elf(int fd, uint64_t load_addr, const char *sym,
         */
        phdr = elf_newphdr(e, 1);
        phdr[0].p_type = PT_LOAD;
-       phdr[0].p_offset = 0;
-       phdr[0].p_vaddr = 0;
-       phdr[0].p_paddr = 0;
+       phdr[0].p_offset = GEN_ELF_TEXT_OFFSET;
+       phdr[0].p_vaddr = GEN_ELF_TEXT_OFFSET;
+       phdr[0].p_paddr = GEN_ELF_TEXT_OFFSET;
        phdr[0].p_filesz = csize;
        phdr[0].p_memsz = csize;
        phdr[0].p_flags = PF_X | PF_R;
--

2.43.0.472.g3155946c3a-goog

author maintainer
#3 lore raw

PATCH 2/3 perf unwind-libdw: Handle JIT-generated DSOs properly

In reply to Namhyung Kim

Usually DSOs are mapped from the beginning of the file, so the base address of the DSO can be calculated by map->start - map->pgoff.

However, JIT DSOs which are generated by perf inject -j, are mapped only the code segment. This makes unwind-libdw code confusing and rejects processing unwinds in the JIT DSOs. It should use the map start address as base for them to fix the confusion.

Fixes 1fe627da3033 ("perf unwind: Take pgoff into account when reporting elf to libdwfl")
---
 tools/perf/util/unwind-libdw.c | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)
tools/perf/util/unwind-libdw.c +17 -4
index 8554db3fc0d7..6013335a8dae 100644
@@ -46,6 +46,7 @@ static int __report_module(struct addr_location *al, u64 ip,
 {
 	Dwfl_Module *mod;
 	struct dso *dso = NULL;
+	Dwarf_Addr base;
 	/*
 	 * Some callers will use al->sym, so we can't just use the
 	 * cheaper thread__find_map() here.
@@ -58,13 +59,25 @@ static int __report_module(struct addr_location *al, u64 ip,
 	if (!dso)
 		return 0;
 
+	/*
+	 * The generated JIT DSO files only map the code segment without
+	 * ELF headers.  Since JIT codes used to be packed in a memory
+	 * segment, calculating the base address using pgoff falls into
+	 * a different code in another DSO.  So just use the map->start
+	 * directly to pick the correct one.
+	 */
+	if (!strncmp(dso->long_name, "/tmp/jitted-", 12))
+		base = map__start(al->map);
+	else
+		base = map__start(al->map) - map__pgoff(al->map);
+
 	mod = dwfl_addrmodule(ui->dwfl, ip);
 	if (mod) {
 		Dwarf_Addr s;
 
 		dwfl_module_info(mod, NULL, &s, NULL, NULL, NULL, NULL, NULL);
-		if (s != map__start(al->map) - map__pgoff(al->map))
-			mod = 0;
+		if (s != base)
+			mod = NULL;
 	}
 
 	if (!mod) {
@@ -72,14 +85,14 @@ static int __report_module(struct addr_location *al, u64 ip,
 
 		__symbol__join_symfs(filename, sizeof(filename), dso->long_name);
 		mod = dwfl_report_elf(ui->dwfl, dso->short_name, filename, -1,
-				      map__start(al->map) - map__pgoff(al->map), false);
+				      base, false);
 	}
 	if (!mod) {
 		char filename[PATH_MAX];
 
 		if (dso__build_id_filename(dso, filename, sizeof(filename), false))
 			mod = dwfl_report_elf(ui->dwfl, dso->short_name, filename, -1,
-					      map__start(al->map) - map__pgoff(al->map), false);
+					      base, false);
 	}
 
 	if (mod) {
Signature · 1 line
2.43.0.472.g3155946c3a-goog
reviewer
#4 lore raw

Re: [PATCH 2/3] perf unwind-libdw: Handle JIT-generated DSOs properly

Quoted 29 lines · Namhyung Kim

Usually DSOs are mapped from the beginning of the file, so the base address of the DSO can be calculated by map->start - map->pgoff.

However, JIT DSOs which are generated by perf inject -j, are mapped only the code segment. This makes unwind-libdw code confusing and rejects processing unwinds in the JIT DSOs. It should use the map start address as base for them to fix the confusion.

Fixes 1fe627da3033 ("perf unwind: Take pgoff into account when reporting elf to libdwfl")
---
 tools/perf/util/unwind-libdw.c | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)
tools/perf/util/unwind-libdw.c +1 -0
index 8554db3fc0d7..6013335a8dae 100644
@@ -46,6 +46,7 @@ static int __report_module(struct addr_location *al, u64 ip,
 {
        Dwfl_Module *mod;
        struct dso *dso = NULL;
+       Dwarf_Addr base;
        /*
         * Some callers will use al->sym, so we can't just use the
         * cheaper thread__find_map() here.
@@ -58,13 +59,25 @@ static int __report_module(struct addr_location *al, u64 ip,
        if (!dso)

tools/perf/util/unwind-libdw.c static int __report_module(struct addr_location *al, u64 ip, · line 69

Quoted 10 lines
tools/perf/util/unwind-libdw.c +8 -0
                return 0;
 
+       /*
+        * The generated JIT DSO files only map the code segment without
+        * ELF headers.  Since JIT codes used to be packed in a memory
+        * segment, calculating the base address using pgoff falls into
+        * a different code in another DSO.  So just use the map->start
+        * directly to pick the correct one.
+        */
+       if (!strncmp(dso->long_name, "/tmp/jitted-", 12))

Perhaps it would be better to test:
dso->symtab_type == DSO_BINARY_TYPE__JAVA_JIT

Thanks,
Ian

Quoted 36 lines
tools/perf/util/unwind-libdw.c +8 -5
+               base = map__start(al->map);
+       else
+               base = map__start(al->map) - map__pgoff(al->map);
+
        mod = dwfl_addrmodule(ui->dwfl, ip);
        if (mod) {
                Dwarf_Addr s;
 
                dwfl_module_info(mod, NULL, &s, NULL, NULL, NULL, NULL, NULL);
-               if (s != map__start(al->map) - map__pgoff(al->map))
-                       mod = 0;
+               if (s != base)
+                       mod = NULL;
        }
 
        if (!mod) {
@@ -72,14 +85,14 @@ static int __report_module(struct addr_location *al, u64 ip,
 
                __symbol__join_symfs(filename, sizeof(filename), dso->long_name);
                mod = dwfl_report_elf(ui->dwfl, dso->short_name, filename, -1,
-                                     map__start(al->map) - map__pgoff(al->map), false);
+                                     base, false);
        }
        if (!mod) {
                char filename[PATH_MAX];
 
                if (dso__build_id_filename(dso, filename, sizeof(filename), false))
                        mod = dwfl_report_elf(ui->dwfl, dso->short_name, filename, -1,
-                                             map__start(al->map) - map__pgoff(al->map), false);
+                                             base, false);
        }
 
        if (mod) {
--

2.43.0.472.g3155946c3a-goog

author maintainer
#5 lore raw

Re: [PATCH 2/3] perf unwind-libdw: Handle JIT-generated DSOs properly

Quoted 33 lines · Ian Rogers
Quoted 31 lines · Namhyung Kim

Usually DSOs are mapped from the beginning of the file, so the base address of the DSO can be calculated by map->start - map->pgoff.

However, JIT DSOs which are generated by perf inject -j, are mapped only the code segment. This makes unwind-libdw code confusing and rejects processing unwinds in the JIT DSOs. It should use the map start address as base for them to fix the confusion.

Fixes 1fe627da3033 ("perf unwind: Take pgoff into account when reporting elf to libdwfl")
---
 tools/perf/util/unwind-libdw.c | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)
tools/perf/util/unwind-libdw.c +1 -0
index 8554db3fc0d7..6013335a8dae 100644
@@ -46,6 +46,7 @@ static int __report_module(struct addr_location *al, u64 ip,
 {
        Dwfl_Module *mod;
        struct dso *dso = NULL;
+       Dwarf_Addr base;
        /*
         * Some callers will use al->sym, so we can't just use the
         * cheaper thread__find_map() here.
@@ -58,13 +59,25 @@ static int __report_module(struct addr_location *al, u64 ip,
        if (!dso)
                return 0;
 
Quoted 11 lines

tools/perf/util/unwind-libdw.c static int __report_module(struct addr_location *al, u64 ip, · line 69

Quoted 8 lines
tools/perf/util/unwind-libdw.c +8 -0
+       /*
+        * The generated JIT DSO files only map the code segment without
+        * ELF headers.  Since JIT codes used to be packed in a memory
+        * segment, calculating the base address using pgoff falls into
+        * a different code in another DSO.  So just use the map->start
+        * directly to pick the correct one.
+        */
+       if (!strncmp(dso->long_name, "/tmp/jitted-", 12))

Perhaps it would be better to test:
dso->symtab_type == DSO_BINARY_TYPE__JAVA_JIT

Well.. it's a little different. The JAVA_JIT type files have "/tmp/perf-" prefix and it's a plain text file (for symbols). While this is an ELF file generated by perf inject -j.

Thanks,
Namhyung

author maintainer
#6 lore raw

PATCH 3/3 perf unwind-libunwind: Fix base address for .eh_frame

In reply to Namhyung Kim

The base address of a DSO mapping should start at the start of the file. Usually DSOs are mapped from the pgoff 0 so it doesn't matter when it uses the start of the map address. But generated DSOs for JIT codes doesn't start from the 0 so it should subtract the offset to calculate the .eh_frame table offsets correctly.

Fixes dc2cf4ca866f ("perf unwind: Fix segbase for ld.lld linked objects")
---
 tools/perf/util/unwind-libunwind-local.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/unwind-libunwind-local.c +1 -1
index c0641882fd2f..5e5c3395a499 100644
@@ -327,7 +327,7 @@ static int read_unwind_spec_eh_frame(struct dso *dso, struct unwind_info *ui,
 
 	maps__for_each_entry(thread__maps(ui->thread), map_node) {
 		struct map *map = map_node->map;
-		u64 start = map__start(map);
+		u64 start = map__start(map) - map__pgoff(map);
 
 		if (map__dso(map) == dso && start < base_addr)
 			base_addr = start;
Signature · 1 line
2.43.0.472.g3155946c3a-goog
reviewer Reviewed-by on v1 3/3
#7 lore raw

Re: [PATCH 3/3] perf unwind-libunwind: Fix base address for .eh_frame

Quoted 9 lines · Namhyung Kim

The base address of a DSO mapping should start at the start of the file. Usually DSOs are mapped from the pgoff 0 so it doesn't matter when it uses the start of the map address. But generated DSOs for JIT codes doesn't start from the 0 so it should subtract the offset to calculate the .eh_frame table offsets correctly.

Fixes dc2cf4ca866f ("perf unwind: Fix segbase for ld.lld linked objects")

Thanks,
Ian

Quoted 20 lines
---
 tools/perf/util/unwind-libunwind-local.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/unwind-libunwind-local.c +1 -2
index c0641882fd2f..5e5c3395a499 100644
@@ -327,7 +327,7 @@ static int read_unwind_spec_eh_frame(struct dso *dso, struct unwind_info *ui,
 
        maps__for_each_entry(thread__maps(ui->thread), map_node) {
                struct map *map = map_node->map;
-               u64 start = map__start(map);
+               u64 start = map__start(map) - map__pgoff(map);
 
                if (map__dso(map) == dso && start < base_addr)
                        base_addr = start;
--

2.43.0.472.g3155946c3a-goog

reviewer
#8 lore raw
In reply to Namhyung Kim
Quoted 7 lines · Namhyung Kim

Hello,

I've found a couple of issues on the unwind code while I'm playing with the JIT-dump code for the CPython. The code assumes normal DSOs mapped from the beginning of the file and aligned to the page size. But it's not true for the JIT-dumped DSOs which are generated for each function.

We have a JIT test in:
https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/tests/shell/test_java_symbol.sh?h=perf-tools-next

It'd be great if we could do similar for CPython.

Thanks,
Ian

Quoted 25 lines

Depending on the JIT implementation, the code address and accompanied ELF info (like ELF file headers and unwind info) can be overlapped to adjacent (JIT-dumped) DSOs. So it should take more care when it calculates the mapping address for the DSO.

It seems these changes need to go to the stable trees but they are changed a lot since then so I'm not sure.

Thanks,
Namhyung

Namhyung Kim (3):
perf genelf: Set ELF program header addresses properly perf unwind-libdw: Handle JIT-generated DSOs properly perf unwind-libunwind: Fix base address for .eh_frame

 tools/perf/util/genelf.c                 |  6 +++---
 tools/perf/util/unwind-libdw.c           | 21 +++++++++++++++++----
 tools/perf/util/unwind-libunwind-local.c |  2 +-
 3 files changed, 21 insertions(+), 8 deletions(-)

--
2.43.0.472.g3155946c3a-goog

#9 lore raw
Quoted 1 line

It'd be great if we could do similar for CPython.

We (the CPython team) plan to release the perf jitdump support for Python 3.13 (around next October) so you will need to build from source or wait until is officially released for that test.

Pablo

author maintainer
#10 lore raw
Quoted 6 lines · Pablo Galindo Salgado
Quoted 2 lines

It'd be great if we could do similar for CPython.

We (the CPython team) plan to release the perf jitdump support for Python 3.13 (around next October) so you will need to build from source or wait until is officially released for that test.

Okay, I hope we can have all the fixes asap and most distros ship the latest version by then.

Thanks,
Namhyung

maintainer Applied
#11 lore raw
In reply to Namhyung Kim

Em Mon, Dec 11, 2023 at 11:05:43PM -0800, Namhyung Kim escreveu:

Quoted 7 lines

Hello,

I've found a couple of issues on the unwind code while I'm playing with the JIT-dump code for the CPython. The code assumes normal DSOs mapped from the beginning of the file and aligned to the page size. But it's not true for the JIT-dumped DSOs which are generated for each function.

Quoted 8 lines

Depending on the JIT implementation, the code address and accompanied ELF info (like ELF file headers and unwind info) can be overlapped to adjacent (JIT-dumped) DSOs. So it should take more care when it calculates the mapping address for the DSO.

It seems these changes need to go to the stable trees but they are changed a lot since then so I'm not sure.

Thanks, applied to perf-tools-next.

- Arnaldo

Quoted 14 lines

Namhyung Kim (3):
perf genelf: Set ELF program header addresses properly perf unwind-libdw: Handle JIT-generated DSOs properly perf unwind-libunwind: Fix base address for .eh_frame

 tools/perf/util/genelf.c                 |  6 +++---
 tools/perf/util/unwind-libdw.c           | 21 +++++++++++++++++----
 tools/perf/util/unwind-libunwind-local.c |  2 +-
 3 files changed, 21 insertions(+), 8 deletions(-)
Signature · 1 line
2.43.0.472.g3155946c3a-goog
Signature · 1 line
- Arnaldo