Skip to contentSkip to conversation

scripts: fix faddr2line to work on last symbol

PATCH In review NeilBrown 3 participants 4 messages Oct 12, 2017 FADDR2LINE

lore
Share & tools
lore

NeilBrown

author
#0 lore raw

PATCH 1/1 scripts: fix faddr2line to work on last symbol

If faddr2line is given a function name which is the last one listed by "nm -n", it will fail because it never finds the next symbol.

So teach the awk script to catch that possibility, and use 'size' to provide the end point of the last function.

---
 scripts/faddr2line | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
scripts/faddr2line +3 -2
index 29df825d375c..2f6ce802397d 100755
@@ -103,11 +103,12 @@ __faddr2line() {
 
 	# Go through each of the object's symbols which match the func name.
 	# In rare cases there might be duplicates.
+	file_end=$(size -Ax $objfile | awk '$1 == ".text" {print $2}')
 	while read symbol; do
 		local fields=($symbol)
 		local sym_base=0x${fields[0]}
 		local sym_type=${fields[1]}
-		local sym_end=0x${fields[3]}
+		local sym_end=${fields[3]}
 
 		# calculate the size
 		local sym_size=$(($sym_end - $sym_base))
@@ -157,7 +158,7 @@ __faddr2line() {
 		addr2line -fpie $objfile $addr | sed "s; $dir_prefix\(\./\)*; ;"
 		DONE=1
 
-	done < <(nm -n $objfile | awk -v fn=$func '$3 == fn { found=1; line=$0; start=$1; next } found == 1 { found=0; print line, $1 }')
+	done < <(nm -n $objfile | awk -v fn=$func -v end=$file_end '$3 == fn { found=1; line=$0; start=$1; next } found == 1 { found=0; print line, "0x"$1 } END {if (found == 1) print line, end; }')
 }
 
 [[ $# -lt 2 ]] && usage
Signature · 1 line
2.14.0.rc0.dirty
#1 lore raw
Quoted 10 lines · NeilBrown

If faddr2line is given a function name which is the last one listed by "nm -n", it will fail because it never finds the next symbol.

So teach the awk script to catch that possibility, and use 'size' to provide the end point of the last function.

Thanks. I'm assuming you saw this issue with a .o file, and not with vmlinux? Because on my vmlinux, the last symbol isn't a text symbol.

This patch is mostly ok, but it will only work if the last symbol is in the .text section (which will probably be true in most cases).

But that makes me realize that this script has even deeper problems with .o files.

With vmlinux, the symbols have been assigned absolute addresses, so it makes sense to sort them by address there, and the symbol sorting works as expected.

But with .o files, they _haven't_ been assigned absolute addresses. Instead they just have section offsets, which is what nm prints. So symbols in different sections will be overlaid. For example:

  $ nm -n kernel/fork.o |grep "0000000000000000 [tT]"
  0000000000000000 t coredump_filter_setup
  0000000000000000 T get_task_mm
  0000000000000000 t set_ti_thread_flag
  0000000000000000 t __setup_coredump_filter_setup
  0000000000000000 t __setup_str_coredump_filter_setup

Here coredump_filter_setup() is at offset 0 in .init.text, set_ti_thread_flag() is at offset 0 in .text.text.unlikely, and get_task_mm() is at offset 0 in .text. That confuses the script:

  $ scripts/faddr2line kernel/fork.o get_task_mm+0x1
  bad symbol size: base: 0x0000000000000000 end: 0x0000000000000000

We need to refactor the script a bit to be more section-aware. Instead of nm, I think it will need to use objdump or readelf, since nm doesn't seem to have an option for dumping the section name.

After fixing that, we can then fix the issue you found with the last symbol.

I can give it a shot, though it may be a few weeks (probably post-Prague).

Signature · 1 line
Josh
#2 lore raw
Quoted 4 lines · Josh Poimboeuf

We need to refactor the script a bit to be more section-aware. Instead of nm, I think it will need to use objdump or readelf, since nm doesn't seem to have an option for dumping the section name.

Fair enough, but I'll apply Neil's patch in the meantime as a stopgap.

Linus

author
#3 lore raw
In reply to Josh Poimboeuf
Quoted 6 lines · Josh Poimboeuf
Quoted 5 lines · NeilBrown

If faddr2line is given a function name which is the last one listed by "nm -n", it will fail because it never finds the next symbol.

Quoted 8 lines
Quoted 5 lines

So teach the awk script to catch that possibility, and use 'size' to provide the end point of the last function.

Thanks. I'm assuming you saw this issue with a .o file, and not with vmlinux? Because on my vmlinux, the last symbol isn't a text symbol.

Yes, I was using a .o

Thanks for looking further into this - there certainly are subtleties that I missed.

Thanks,
NeilBrown