faddr2line 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. #
  4. # Translate stack dump function offsets.
  5. #
  6. # addr2line doesn't work with KASLR addresses. This works similarly to
  7. # addr2line, but instead takes the 'func+0x123' format as input:
  8. #
  9. # $ ./scripts/faddr2line ~/k/vmlinux meminfo_proc_show+0x5/0x568
  10. # meminfo_proc_show+0x5/0x568:
  11. # meminfo_proc_show at fs/proc/meminfo.c:27
  12. #
  13. # If the address is part of an inlined function, the full inline call chain is
  14. # printed:
  15. #
  16. # $ ./scripts/faddr2line ~/k/vmlinux native_write_msr+0x6/0x27
  17. # native_write_msr+0x6/0x27:
  18. # arch_static_branch at arch/x86/include/asm/msr.h:121
  19. # (inlined by) static_key_false at include/linux/jump_label.h:125
  20. # (inlined by) native_write_msr at arch/x86/include/asm/msr.h:125
  21. #
  22. # The function size after the '/' in the input is optional, but recommended.
  23. # It's used to help disambiguate any duplicate symbol names, which can occur
  24. # rarely. If the size is omitted for a duplicate symbol then it's possible for
  25. # multiple code sites to be printed:
  26. #
  27. # $ ./scripts/faddr2line ~/k/vmlinux raw_ioctl+0x5
  28. # raw_ioctl+0x5/0x20:
  29. # raw_ioctl at drivers/char/raw.c:122
  30. #
  31. # raw_ioctl+0x5/0xb1:
  32. # raw_ioctl at net/ipv4/raw.c:876
  33. #
  34. # Multiple addresses can be specified on a single command line:
  35. #
  36. # $ ./scripts/faddr2line ~/k/vmlinux type_show+0x10/45 free_reserved_area+0x90
  37. # type_show+0x10/0x2d:
  38. # type_show at drivers/video/backlight/backlight.c:213
  39. #
  40. # free_reserved_area+0x90/0x123:
  41. # free_reserved_area at mm/page_alloc.c:6429 (discriminator 2)
  42. set -o errexit
  43. set -o nounset
  44. usage() {
  45. echo "usage: faddr2line [--list] <object file> <func+offset> <func+offset>..." >&2
  46. exit 1
  47. }
  48. warn() {
  49. echo "$1" >&2
  50. }
  51. die() {
  52. echo "ERROR: $1" >&2
  53. exit 1
  54. }
  55. READELF="${CROSS_COMPILE:-}readelf"
  56. ADDR2LINE="${CROSS_COMPILE:-}addr2line"
  57. AWK="awk"
  58. command -v ${AWK} >/dev/null 2>&1 || die "${AWK} isn't installed"
  59. command -v ${READELF} >/dev/null 2>&1 || die "${READELF} isn't installed"
  60. command -v ${ADDR2LINE} >/dev/null 2>&1 || die "${ADDR2LINE} isn't installed"
  61. # Try to figure out the source directory prefix so we can remove it from the
  62. # addr2line output. HACK ALERT: This assumes that start_kernel() is in
  63. # init/main.c! This only works for vmlinux. Otherwise it falls back to
  64. # printing the absolute path.
  65. find_dir_prefix() {
  66. local objfile=$1
  67. local start_kernel_addr=$(${READELF} --symbols --wide $objfile | sed 's/\[.*\]//' |
  68. ${AWK} '$8 == "start_kernel" {printf "0x%s", $2}')
  69. [[ -z $start_kernel_addr ]] && return
  70. local file_line=$(${ADDR2LINE} -e $objfile $start_kernel_addr)
  71. [[ -z $file_line ]] && return
  72. local prefix=${file_line%init/main.c:*}
  73. if [[ -z $prefix ]] || [[ $prefix = $file_line ]]; then
  74. return
  75. fi
  76. DIR_PREFIX=$prefix
  77. return 0
  78. }
  79. __faddr2line() {
  80. local objfile=$1
  81. local func_addr=$2
  82. local dir_prefix=$3
  83. local print_warnings=$4
  84. local sym_name=${func_addr%+*}
  85. local func_offset=${func_addr#*+}
  86. func_offset=${func_offset%/*}
  87. local user_size=
  88. local file_type
  89. local is_vmlinux=0
  90. [[ $func_addr =~ "/" ]] && user_size=${func_addr#*/}
  91. if [[ -z $sym_name ]] || [[ -z $func_offset ]] || [[ $sym_name = $func_addr ]]; then
  92. warn "bad func+offset $func_addr"
  93. DONE=1
  94. return
  95. fi
  96. # vmlinux uses absolute addresses in the section table rather than
  97. # section offsets.
  98. local file_type=$(${READELF} --file-header $objfile |
  99. ${AWK} '$1 == "Type:" { print $2; exit }')
  100. if [[ $file_type = "EXEC" ]] || [[ $file_type == "DYN" ]]; then
  101. is_vmlinux=1
  102. fi
  103. # Go through each of the object's symbols which match the func name.
  104. # In rare cases there might be duplicates, in which case we print all
  105. # matches.
  106. while read line; do
  107. local fields=($line)
  108. local sym_addr=0x${fields[1]}
  109. local sym_elf_size=${fields[2]}
  110. local sym_sec=${fields[6]}
  111. local sec_size
  112. local sec_name
  113. # Get the section size:
  114. sec_size=$(${READELF} --section-headers --wide $objfile |
  115. sed 's/\[ /\[/' |
  116. ${AWK} -v sec=$sym_sec '$1 == "[" sec "]" { print "0x" $6; exit }')
  117. if [[ -z $sec_size ]]; then
  118. warn "bad section size: section: $sym_sec"
  119. DONE=1
  120. return
  121. fi
  122. # Get the section name:
  123. sec_name=$(${READELF} --section-headers --wide $objfile |
  124. sed 's/\[ /\[/' |
  125. ${AWK} -v sec=$sym_sec '$1 == "[" sec "]" { print $2; exit }')
  126. if [[ -z $sec_name ]]; then
  127. warn "bad section name: section: $sym_sec"
  128. DONE=1
  129. return
  130. fi
  131. # Calculate the symbol size.
  132. #
  133. # Unfortunately we can't use the ELF size, because kallsyms
  134. # also includes the padding bytes in its size calculation. For
  135. # kallsyms, the size calculation is the distance between the
  136. # symbol and the next symbol in a sorted list.
  137. local sym_size
  138. local cur_sym_addr
  139. local found=0
  140. while read line; do
  141. local fields=($line)
  142. cur_sym_addr=0x${fields[1]}
  143. local cur_sym_elf_size=${fields[2]}
  144. local cur_sym_name=${fields[7]:-}
  145. if [[ $cur_sym_addr = $sym_addr ]] &&
  146. [[ $cur_sym_elf_size = $sym_elf_size ]] &&
  147. [[ $cur_sym_name = $sym_name ]]; then
  148. found=1
  149. continue
  150. fi
  151. if [[ $found = 1 ]]; then
  152. sym_size=$(($cur_sym_addr - $sym_addr))
  153. [[ $sym_size -lt $sym_elf_size ]] && continue;
  154. found=2
  155. break
  156. fi
  157. done < <(${READELF} --symbols --wide $objfile | sed 's/\[.*\]//' | ${AWK} -v sec=$sym_sec '$7 == sec' | sort --key=2)
  158. if [[ $found = 0 ]]; then
  159. warn "can't find symbol: sym_name: $sym_name sym_sec: $sym_sec sym_addr: $sym_addr sym_elf_size: $sym_elf_size"
  160. DONE=1
  161. return
  162. fi
  163. # If nothing was found after the symbol, assume it's the last
  164. # symbol in the section.
  165. [[ $found = 1 ]] && sym_size=$(($sec_size - $sym_addr))
  166. if [[ -z $sym_size ]] || [[ $sym_size -le 0 ]]; then
  167. warn "bad symbol size: sym_addr: $sym_addr cur_sym_addr: $cur_sym_addr"
  168. DONE=1
  169. return
  170. fi
  171. sym_size=0x$(printf %x $sym_size)
  172. # Calculate the address from user-supplied offset:
  173. local addr=$(($sym_addr + $func_offset))
  174. if [[ -z $addr ]] || [[ $addr = 0 ]]; then
  175. warn "bad address: $sym_addr + $func_offset"
  176. DONE=1
  177. return
  178. fi
  179. addr=0x$(printf %x $addr)
  180. # If the user provided a size, make sure it matches the symbol's size:
  181. if [[ -n $user_size ]] && [[ $user_size -ne $sym_size ]]; then
  182. [[ $print_warnings = 1 ]] &&
  183. echo "skipping $sym_name address at $addr due to size mismatch ($user_size != $sym_size)"
  184. continue;
  185. fi
  186. # Make sure the provided offset is within the symbol's range:
  187. if [[ $func_offset -gt $sym_size ]]; then
  188. [[ $print_warnings = 1 ]] &&
  189. echo "skipping $sym_name address at $addr due to size mismatch ($func_offset > $sym_size)"
  190. continue
  191. fi
  192. # In case of duplicates or multiple addresses specified on the
  193. # cmdline, separate multiple entries with a blank line:
  194. [[ $FIRST = 0 ]] && echo
  195. FIRST=0
  196. echo "$sym_name+$func_offset/$sym_size:"
  197. # Pass section address to addr2line and strip absolute paths
  198. # from the output:
  199. local args="--functions --pretty-print --inlines --exe=$objfile"
  200. [[ $is_vmlinux = 0 ]] && args="$args --section=$sec_name"
  201. local output=$(${ADDR2LINE} $args $addr | sed "s; $dir_prefix\(\./\)*; ;")
  202. [[ -z $output ]] && continue
  203. # Default output (non --list):
  204. if [[ $LIST = 0 ]]; then
  205. echo "$output" | while read -r line
  206. do
  207. echo $line
  208. done
  209. DONE=1;
  210. continue
  211. fi
  212. # For --list, show each line with its corresponding source code:
  213. echo "$output" | while read -r line
  214. do
  215. echo
  216. echo $line
  217. n=$(echo $line | sed 's/.*:\([0-9]\+\).*/\1/g')
  218. n1=$[$n-5]
  219. n2=$[$n+5]
  220. f=$(echo $line | sed 's/.*at \(.\+\):.*/\1/g')
  221. ${AWK} 'NR>=strtonum("'$n1'") && NR<=strtonum("'$n2'") { if (NR=='$n') printf(">%d<", NR); else printf(" %d ", NR); printf("\t%s\n", $0)}' $f
  222. done
  223. DONE=1
  224. done < <(${READELF} --symbols --wide $objfile | sed 's/\[.*\]//' | ${AWK} -v fn=$sym_name '$4 == "FUNC" && $8 == fn')
  225. }
  226. [[ $# -lt 2 ]] && usage
  227. objfile=$1
  228. LIST=0
  229. [[ "$objfile" == "--list" ]] && LIST=1 && shift && objfile=$1
  230. [[ ! -f $objfile ]] && die "can't find objfile $objfile"
  231. shift
  232. DIR_PREFIX=supercalifragilisticexpialidocious
  233. find_dir_prefix $objfile
  234. FIRST=1
  235. while [[ $# -gt 0 ]]; do
  236. func_addr=$1
  237. shift
  238. # print any matches found
  239. DONE=0
  240. __faddr2line $objfile $func_addr $DIR_PREFIX 0
  241. # if no match was found, print warnings
  242. if [[ $DONE = 0 ]]; then
  243. __faddr2line $objfile $func_addr $DIR_PREFIX 1
  244. warn "no match for $func_addr"
  245. fi
  246. done