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