faddr2line 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. GREP="grep"
  59. command -v ${AWK} >/dev/null 2>&1 || die "${AWK} isn't installed"
  60. command -v ${READELF} >/dev/null 2>&1 || die "${READELF} isn't installed"
  61. command -v ${ADDR2LINE} >/dev/null 2>&1 || die "${ADDR2LINE} isn't installed"
  62. # Try to figure out the source directory prefix so we can remove it from the
  63. # addr2line output. HACK ALERT: This assumes that start_kernel() is in
  64. # init/main.c! This only works for vmlinux. Otherwise it falls back to
  65. # printing the absolute path.
  66. find_dir_prefix() {
  67. local objfile=$1
  68. local start_kernel_addr=$(${READELF} --symbols --wide $objfile | sed 's/\[.*\]//' |
  69. ${AWK} '$8 == "start_kernel" {printf "0x%s", $2}')
  70. [[ -z $start_kernel_addr ]] && return
  71. local file_line=$(${ADDR2LINE} -e $objfile $start_kernel_addr)
  72. [[ -z $file_line ]] && return
  73. local prefix=${file_line%init/main.c:*}
  74. if [[ -z $prefix ]] || [[ $prefix = $file_line ]]; then
  75. return
  76. fi
  77. DIR_PREFIX=$prefix
  78. return 0
  79. }
  80. __faddr2line() {
  81. local objfile=$1
  82. local func_addr=$2
  83. local dir_prefix=$3
  84. local print_warnings=$4
  85. local sym_name=${func_addr%+*}
  86. local func_offset=${func_addr#*+}
  87. func_offset=${func_offset%/*}
  88. local user_size=
  89. local file_type
  90. local is_vmlinux=0
  91. [[ $func_addr =~ "/" ]] && user_size=${func_addr#*/}
  92. if [[ -z $sym_name ]] || [[ -z $func_offset ]] || [[ $sym_name = $func_addr ]]; then
  93. warn "bad func+offset $func_addr"
  94. DONE=1
  95. return
  96. fi
  97. # vmlinux uses absolute addresses in the section table rather than
  98. # section offsets.
  99. local file_type=$(${READELF} --file-header $objfile |
  100. ${AWK} '$1 == "Type:" { print $2; exit }')
  101. if [[ $file_type = "EXEC" ]] || [[ $file_type == "DYN" ]]; then
  102. is_vmlinux=1
  103. fi
  104. # Go through each of the object's symbols which match the func name.
  105. # In rare cases there might be duplicates, in which case we print all
  106. # matches.
  107. while read line; do
  108. local fields=($line)
  109. local sym_addr=0x${fields[1]}
  110. local sym_elf_size=${fields[2]}
  111. local sym_sec=${fields[6]}
  112. local sec_size
  113. local sec_name
  114. # Get the section size:
  115. sec_size=$(${READELF} --section-headers --wide $objfile |
  116. sed 's/\[ /\[/' |
  117. ${AWK} -v sec=$sym_sec '$1 == "[" sec "]" { print "0x" $6; exit }')
  118. if [[ -z $sec_size ]]; then
  119. warn "bad section size: section: $sym_sec"
  120. DONE=1
  121. return
  122. fi
  123. # Get the section name:
  124. sec_name=$(${READELF} --section-headers --wide $objfile |
  125. sed 's/\[ /\[/' |
  126. ${AWK} -v sec=$sym_sec '$1 == "[" sec "]" { print $2; exit }')
  127. if [[ -z $sec_name ]]; then
  128. warn "bad section name: section: $sym_sec"
  129. DONE=1
  130. return
  131. fi
  132. # Calculate the symbol size.
  133. #
  134. # Unfortunately we can't use the ELF size, because kallsyms
  135. # also includes the padding bytes in its size calculation. For
  136. # kallsyms, the size calculation is the distance between the
  137. # symbol and the next symbol in a sorted list.
  138. local sym_size
  139. local cur_sym_addr
  140. local found=0
  141. while read line; do
  142. local fields=($line)
  143. cur_sym_addr=0x${fields[1]}
  144. local cur_sym_elf_size=${fields[2]}
  145. local cur_sym_name=${fields[7]:-}
  146. if [[ $cur_sym_addr = $sym_addr ]] &&
  147. [[ $cur_sym_elf_size = $sym_elf_size ]] &&
  148. [[ $cur_sym_name = $sym_name ]]; then
  149. found=1
  150. continue
  151. fi
  152. if [[ $found = 1 ]]; then
  153. sym_size=$(($cur_sym_addr - $sym_addr))
  154. [[ $sym_size -lt $sym_elf_size ]] && continue;
  155. found=2
  156. break
  157. fi
  158. done < <(${READELF} --symbols --wide $objfile | sed 's/\[.*\]//' | ${AWK} -v sec=$sym_sec '$7 == sec' | sort --key=2)
  159. if [[ $found = 0 ]]; then
  160. warn "can't find symbol: sym_name: $sym_name sym_sec: $sym_sec sym_addr: $sym_addr sym_elf_size: $sym_elf_size"
  161. DONE=1
  162. return
  163. fi
  164. # If nothing was found after the symbol, assume it's the last
  165. # symbol in the section.
  166. [[ $found = 1 ]] && sym_size=$(($sec_size - $sym_addr))
  167. if [[ -z $sym_size ]] || [[ $sym_size -le 0 ]]; then
  168. warn "bad symbol size: sym_addr: $sym_addr cur_sym_addr: $cur_sym_addr"
  169. DONE=1
  170. return
  171. fi
  172. sym_size=0x$(printf %x $sym_size)
  173. # Calculate the address from user-supplied offset:
  174. local addr=$(($sym_addr + $func_offset))
  175. if [[ -z $addr ]] || [[ $addr = 0 ]]; then
  176. warn "bad address: $sym_addr + $func_offset"
  177. DONE=1
  178. return
  179. fi
  180. addr=0x$(printf %x $addr)
  181. # If the user provided a size, make sure it matches the symbol's size:
  182. if [[ -n $user_size ]] && [[ $user_size -ne $sym_size ]]; then
  183. [[ $print_warnings = 1 ]] &&
  184. echo "skipping $sym_name address at $addr due to size mismatch ($user_size != $sym_size)"
  185. continue;
  186. fi
  187. # Make sure the provided offset is within the symbol's range:
  188. if [[ $func_offset -gt $sym_size ]]; then
  189. [[ $print_warnings = 1 ]] &&
  190. echo "skipping $sym_name address at $addr due to size mismatch ($func_offset > $sym_size)"
  191. continue
  192. fi
  193. # In case of duplicates or multiple addresses specified on the
  194. # cmdline, separate multiple entries with a blank line:
  195. [[ $FIRST = 0 ]] && echo
  196. FIRST=0
  197. echo "$sym_name+$func_offset/$sym_size:"
  198. # Pass section address to addr2line and strip absolute paths
  199. # from the output:
  200. local args="--functions --pretty-print --inlines --exe=$objfile"
  201. [[ $is_vmlinux = 0 ]] && args="$args --section=$sec_name"
  202. local output=$(${ADDR2LINE} $args $addr | sed "s; $dir_prefix\(\./\)*; ;")
  203. [[ -z $output ]] && continue
  204. # Default output (non --list):
  205. if [[ $LIST = 0 ]]; then
  206. echo "$output" | while read -r line
  207. do
  208. echo $line
  209. done
  210. DONE=1;
  211. continue
  212. fi
  213. # For --list, show each line with its corresponding source code:
  214. echo "$output" | while read -r line
  215. do
  216. echo
  217. echo $line
  218. n=$(echo $line | sed 's/.*:\([0-9]\+\).*/\1/g')
  219. n1=$[$n-5]
  220. n2=$[$n+5]
  221. f=$(echo $line | sed 's/.*at \(.\+\):.*/\1/g')
  222. ${AWK} 'NR>=strtonum("'$n1'") && NR<=strtonum("'$n2'") { if (NR=='$n') printf(">%d<", NR); else printf(" %d ", NR); printf("\t%s\n", $0)}' $f
  223. done
  224. DONE=1
  225. done < <(${READELF} --symbols --wide $objfile | sed 's/\[.*\]//' | ${AWK} -v fn=$sym_name '$4 == "FUNC" && $8 == fn')
  226. }
  227. [[ $# -lt 2 ]] && usage
  228. objfile=$1
  229. LIST=0
  230. [[ "$objfile" == "--list" ]] && LIST=1 && shift && objfile=$1
  231. [[ ! -f $objfile ]] && die "can't find objfile $objfile"
  232. shift
  233. ${READELF} --section-headers --wide $objfile | ${GREP} -q '\.debug_info' || die "CONFIG_DEBUG_INFO not enabled"
  234. DIR_PREFIX=supercalifragilisticexpialidocious
  235. find_dir_prefix $objfile
  236. FIRST=1
  237. while [[ $# -gt 0 ]]; do
  238. func_addr=$1
  239. shift
  240. # print any matches found
  241. DONE=0
  242. __faddr2line $objfile $func_addr $DIR_PREFIX 0
  243. # if no match was found, print warnings
  244. if [[ $DONE = 0 ]]; then
  245. __faddr2line $objfile $func_addr $DIR_PREFIX 1
  246. warn "no match for $func_addr"
  247. fi
  248. done