faddr2line 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. #!/usr/bin/env 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. UTIL_SUFFIX=""
  56. if [[ "${LLVM:-}" == "" ]]; then
  57. UTIL_PREFIX=${CROSS_COMPILE:-}
  58. else
  59. UTIL_PREFIX=llvm-
  60. if [[ "${LLVM}" == *"/" ]]; then
  61. UTIL_PREFIX=${LLVM}${UTIL_PREFIX}
  62. elif [[ "${LLVM}" == "-"* ]]; then
  63. UTIL_SUFFIX=${LLVM}
  64. fi
  65. fi
  66. READELF="${UTIL_PREFIX}readelf${UTIL_SUFFIX}"
  67. ADDR2LINE="${UTIL_PREFIX}addr2line${UTIL_SUFFIX}"
  68. AWK="awk"
  69. GREP="grep"
  70. # Enforce ASCII-only output from tools like readelf
  71. # ensuring sed processes strings correctly.
  72. export LANG=C
  73. command -v ${AWK} >/dev/null 2>&1 || die "${AWK} isn't installed"
  74. command -v ${READELF} >/dev/null 2>&1 || die "${READELF} isn't installed"
  75. command -v ${ADDR2LINE} >/dev/null 2>&1 || die "${ADDR2LINE} isn't installed"
  76. # Try to figure out the source directory prefix so we can remove it from the
  77. # addr2line output. HACK ALERT: This assumes that start_kernel() is in
  78. # init/main.c! This only works for vmlinux. Otherwise it falls back to
  79. # printing the absolute path.
  80. find_dir_prefix() {
  81. local start_kernel_addr=$(echo "${ELF_SYMS}" | sed 's/\[.*\]//' |
  82. ${AWK} '$8 == "start_kernel" {printf "0x%s", $2}')
  83. [[ -z $start_kernel_addr ]] && return
  84. run_addr2line ${start_kernel_addr} ""
  85. [[ -z $ADDR2LINE_OUT ]] && return
  86. local file_line=${ADDR2LINE_OUT#* at }
  87. if [[ -z $file_line ]] || [[ $file_line = $ADDR2LINE_OUT ]]; then
  88. return
  89. fi
  90. local prefix=${file_line%init/main.c:*}
  91. if [[ -z $prefix ]] || [[ $prefix = $file_line ]]; then
  92. return
  93. fi
  94. DIR_PREFIX=$prefix
  95. return 0
  96. }
  97. run_readelf() {
  98. local objfile=$1
  99. local tmpfile
  100. tmpfile=$(mktemp)
  101. ${READELF} --file-header --section-headers --symbols --wide "$objfile" > "$tmpfile"
  102. # This assumes that readelf first prints the file header, then the section headers, then the symbols.
  103. # Note: It seems that GNU readelf does not prefix section headers with the "There are X section headers"
  104. # line when multiple options are given, so let's also match with the "Section Headers:" line.
  105. ELF_FILEHEADER=$(sed -n '/There are [0-9]* section headers, starting at offset\|Section Headers:/q;p' "$tmpfile")
  106. ELF_SECHEADERS=$(sed -n '/There are [0-9]* section headers, starting at offset\|Section Headers:/,$p' "$tmpfile" | sed -n '/Symbol table .* contains [0-9]* entries:/q;p')
  107. ELF_SYMS=$(sed -n '/Symbol table .* contains [0-9]* entries:/,$p' "$tmpfile")
  108. rm -f -- "$tmpfile"
  109. }
  110. check_vmlinux() {
  111. # vmlinux uses absolute addresses in the section table rather than
  112. # section offsets.
  113. IS_VMLINUX=0
  114. local file_type=$(echo "${ELF_FILEHEADER}" |
  115. ${AWK} '$1 == "Type:" { print $2; exit }')
  116. if [[ $file_type = "EXEC" ]] || [[ $file_type == "DYN" ]]; then
  117. IS_VMLINUX=1
  118. fi
  119. }
  120. init_addr2line() {
  121. local objfile=$1
  122. check_vmlinux
  123. ADDR2LINE_ARGS="--functions --pretty-print --inlines --addresses --exe=$objfile"
  124. if [[ $IS_VMLINUX = 1 ]]; then
  125. # If the executable file is vmlinux, we don't pass section names to
  126. # addr2line, so we can launch it now as a single long-running process.
  127. coproc ADDR2LINE_PROC (${ADDR2LINE} ${ADDR2LINE_ARGS})
  128. fi
  129. }
  130. run_addr2line() {
  131. local addr=$1
  132. local sec_name=$2
  133. if [[ $IS_VMLINUX = 1 ]]; then
  134. # We send to the addr2line process: (1) the address, then (2) a sentinel
  135. # value, i.e., something that can't be interpreted as a valid address
  136. # (i.e., ","). This causes addr2line to write out: (1) the answer for
  137. # our address, then (2) either "?? ??:0" or "0x0...0: ..." (if
  138. # using binutils' addr2line), or "," (if using LLVM's addr2line).
  139. echo ${addr} >& "${ADDR2LINE_PROC[1]}"
  140. echo "," >& "${ADDR2LINE_PROC[1]}"
  141. local first_line
  142. read -r first_line <& "${ADDR2LINE_PROC[0]}"
  143. ADDR2LINE_OUT=$(echo "${first_line}" | sed 's/^0x[0-9a-fA-F]*: //')
  144. while read -r line <& "${ADDR2LINE_PROC[0]}"; do
  145. if [[ "$line" == "?? ??:0" ]] || [[ "$line" == "," ]] || [[ $(echo "$line" | ${GREP} "^0x00*: ") ]]; then
  146. break
  147. fi
  148. ADDR2LINE_OUT+=$'\n'$(echo "$line" | sed 's/^0x[0-9a-fA-F]*: //')
  149. done
  150. else
  151. # Run addr2line as a single invocation.
  152. local sec_arg
  153. [[ -z $sec_name ]] && sec_arg="" || sec_arg="--section=${sec_name}"
  154. ADDR2LINE_OUT=$(${ADDR2LINE} ${ADDR2LINE_ARGS} ${sec_arg} ${addr} | sed 's/^0x[0-9a-fA-F]*: //')
  155. fi
  156. }
  157. __faddr2line() {
  158. local objfile=$1
  159. local func_addr=$2
  160. local dir_prefix=$3
  161. local print_warnings=$4
  162. local sym_name=${func_addr%+*}
  163. local func_offset=${func_addr#*+}
  164. func_offset=${func_offset%/*}
  165. local user_size=
  166. [[ $func_addr =~ "/" ]] && user_size=${func_addr#*/}
  167. if [[ -z $sym_name ]] || [[ -z $func_offset ]] || [[ $sym_name = $func_addr ]]; then
  168. warn "bad func+offset $func_addr"
  169. DONE=1
  170. return
  171. fi
  172. # Go through each of the object's symbols which match the func name.
  173. # In rare cases there might be duplicates, in which case we print all
  174. # matches.
  175. while read line; do
  176. local fields=($line)
  177. local sym_addr=0x${fields[1]}
  178. local sym_elf_size=${fields[2]}
  179. local sym_sec=${fields[6]}
  180. local sec_size
  181. local sec_name
  182. # Get the section size:
  183. sec_size=$(echo "${ELF_SECHEADERS}" | sed 's/\[ /\[/' |
  184. ${AWK} -v sec=$sym_sec '$1 == "[" sec "]" { print "0x" $6; exit }')
  185. if [[ -z $sec_size ]]; then
  186. warn "bad section size: section: $sym_sec"
  187. DONE=1
  188. return
  189. fi
  190. # Get the section name:
  191. sec_name=$(echo "${ELF_SECHEADERS}" | sed 's/\[ /\[/' |
  192. ${AWK} -v sec=$sym_sec '$1 == "[" sec "]" { print $2; exit }')
  193. if [[ -z $sec_name ]]; then
  194. warn "bad section name: section: $sym_sec"
  195. DONE=1
  196. return
  197. fi
  198. # Calculate the symbol size.
  199. #
  200. # Unfortunately we can't use the ELF size, because kallsyms
  201. # also includes the padding bytes in its size calculation. For
  202. # kallsyms, the size calculation is the distance between the
  203. # symbol and the next symbol in a sorted list.
  204. local sym_size
  205. local cur_sym_addr
  206. local found=0
  207. while read line; do
  208. local fields=($line)
  209. cur_sym_addr=0x${fields[1]}
  210. local cur_sym_elf_size=${fields[2]}
  211. local cur_sym_name=${fields[7]:-}
  212. # is_mapping_symbol(cur_sym_name)
  213. if [[ ${cur_sym_name} =~ ^(\.L|L0|\$) ]]; then
  214. continue
  215. fi
  216. if [[ $cur_sym_addr = $sym_addr ]] &&
  217. [[ $cur_sym_elf_size = $sym_elf_size ]] &&
  218. [[ $cur_sym_name = $sym_name ]]; then
  219. found=1
  220. continue
  221. fi
  222. if [[ $found = 1 ]]; then
  223. sym_size=$(($cur_sym_addr - $sym_addr))
  224. [[ $sym_size -lt $sym_elf_size ]] && continue;
  225. found=2
  226. break
  227. fi
  228. done < <(echo "${ELF_SYMS}" | sed 's/\[.*\]//' | ${AWK} -v sec=$sym_sec '$7 == sec' | sort --key=2)
  229. if [[ $found = 0 ]]; then
  230. warn "can't find symbol: sym_name: $sym_name sym_sec: $sym_sec sym_addr: $sym_addr sym_elf_size: $sym_elf_size"
  231. DONE=1
  232. return
  233. fi
  234. # If nothing was found after the symbol, assume it's the last
  235. # symbol in the section.
  236. [[ $found = 1 ]] && sym_size=$(($sec_size - $sym_addr))
  237. if [[ -z $sym_size ]] || [[ $sym_size -le 0 ]]; then
  238. warn "bad symbol size: sym_addr: $sym_addr cur_sym_addr: $cur_sym_addr"
  239. DONE=1
  240. return
  241. fi
  242. sym_size=0x$(printf %x $sym_size)
  243. # Calculate the address from user-supplied offset:
  244. local addr=$(($sym_addr + $func_offset))
  245. if [[ -z $addr ]] || [[ $addr = 0 ]]; then
  246. warn "bad address: $sym_addr + $func_offset"
  247. DONE=1
  248. return
  249. fi
  250. addr=0x$(printf %x $addr)
  251. # If the user provided a size, make sure it matches the symbol's size:
  252. if [[ -n $user_size ]] && [[ $user_size -ne $sym_size ]]; then
  253. [[ $print_warnings = 1 ]] &&
  254. echo "skipping $sym_name address at $addr due to size mismatch ($user_size != $sym_size)"
  255. continue;
  256. fi
  257. # Make sure the provided offset is within the symbol's range:
  258. if [[ $func_offset -gt $sym_size ]]; then
  259. [[ $print_warnings = 1 ]] &&
  260. echo "skipping $sym_name address at $addr due to size mismatch ($func_offset > $sym_size)"
  261. continue
  262. fi
  263. # In case of duplicates or multiple addresses specified on the
  264. # cmdline, separate multiple entries with a blank line:
  265. [[ $FIRST = 0 ]] && echo
  266. FIRST=0
  267. echo "$sym_name+$func_offset/$sym_size:"
  268. # Pass section address to addr2line and strip absolute paths
  269. # from the output:
  270. run_addr2line $addr $sec_name
  271. local output=$(echo "${ADDR2LINE_OUT}" | sed "s; $dir_prefix\(\./\)*; ;")
  272. [[ -z $output ]] && continue
  273. # Default output (non --list):
  274. if [[ $LIST = 0 ]]; then
  275. echo "$output" | while read -r line
  276. do
  277. echo $line
  278. done
  279. DONE=1;
  280. continue
  281. fi
  282. # For --list, show each line with its corresponding source code:
  283. echo "$output" | while read -r line
  284. do
  285. echo
  286. echo $line
  287. n=$(echo $line | sed 's/.*:\([0-9]\+\).*/\1/g')
  288. n1=$[$n-5]
  289. n2=$[$n+5]
  290. f=$(echo $line | sed 's/.*at \(.\+\):.*/\1/g')
  291. ${AWK} 'NR>=strtonum("'$n1'") && NR<=strtonum("'$n2'") { if (NR=='$n') printf(">%d<", NR); else printf(" %d ", NR); printf("\t%s\n", $0)}' $f
  292. done
  293. DONE=1
  294. done < <(echo "${ELF_SYMS}" | sed 's/\[.*\]//' | ${AWK} -v fn=$sym_name '$8 == fn')
  295. }
  296. [[ $# -lt 2 ]] && usage
  297. objfile=$1
  298. LIST=0
  299. [[ "$objfile" == "--list" ]] && LIST=1 && shift && objfile=$1
  300. [[ ! -f $objfile ]] && die "can't find objfile $objfile"
  301. shift
  302. run_readelf $objfile
  303. echo "${ELF_SECHEADERS}" | ${GREP} -q '\.debug_info' || die "CONFIG_DEBUG_INFO not enabled"
  304. init_addr2line $objfile
  305. DIR_PREFIX=supercalifragilisticexpialidocious
  306. find_dir_prefix
  307. FIRST=1
  308. while [[ $# -gt 0 ]]; do
  309. func_addr=$1
  310. shift
  311. # print any matches found
  312. DONE=0
  313. __faddr2line $objfile $func_addr $DIR_PREFIX 0
  314. # if no match was found, print warnings
  315. if [[ $DONE = 0 ]]; then
  316. __faddr2line $objfile $func_addr $DIR_PREFIX 1
  317. warn "no match for $func_addr"
  318. fi
  319. done