162306a36Sopenharmony_ci#!/bin/bash 262306a36Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0 362306a36Sopenharmony_ci# 462306a36Sopenharmony_ci# Translate stack dump function offsets. 562306a36Sopenharmony_ci# 662306a36Sopenharmony_ci# addr2line doesn't work with KASLR addresses. This works similarly to 762306a36Sopenharmony_ci# addr2line, but instead takes the 'func+0x123' format as input: 862306a36Sopenharmony_ci# 962306a36Sopenharmony_ci# $ ./scripts/faddr2line ~/k/vmlinux meminfo_proc_show+0x5/0x568 1062306a36Sopenharmony_ci# meminfo_proc_show+0x5/0x568: 1162306a36Sopenharmony_ci# meminfo_proc_show at fs/proc/meminfo.c:27 1262306a36Sopenharmony_ci# 1362306a36Sopenharmony_ci# If the address is part of an inlined function, the full inline call chain is 1462306a36Sopenharmony_ci# printed: 1562306a36Sopenharmony_ci# 1662306a36Sopenharmony_ci# $ ./scripts/faddr2line ~/k/vmlinux native_write_msr+0x6/0x27 1762306a36Sopenharmony_ci# native_write_msr+0x6/0x27: 1862306a36Sopenharmony_ci# arch_static_branch at arch/x86/include/asm/msr.h:121 1962306a36Sopenharmony_ci# (inlined by) static_key_false at include/linux/jump_label.h:125 2062306a36Sopenharmony_ci# (inlined by) native_write_msr at arch/x86/include/asm/msr.h:125 2162306a36Sopenharmony_ci# 2262306a36Sopenharmony_ci# The function size after the '/' in the input is optional, but recommended. 2362306a36Sopenharmony_ci# It's used to help disambiguate any duplicate symbol names, which can occur 2462306a36Sopenharmony_ci# rarely. If the size is omitted for a duplicate symbol then it's possible for 2562306a36Sopenharmony_ci# multiple code sites to be printed: 2662306a36Sopenharmony_ci# 2762306a36Sopenharmony_ci# $ ./scripts/faddr2line ~/k/vmlinux raw_ioctl+0x5 2862306a36Sopenharmony_ci# raw_ioctl+0x5/0x20: 2962306a36Sopenharmony_ci# raw_ioctl at drivers/char/raw.c:122 3062306a36Sopenharmony_ci# 3162306a36Sopenharmony_ci# raw_ioctl+0x5/0xb1: 3262306a36Sopenharmony_ci# raw_ioctl at net/ipv4/raw.c:876 3362306a36Sopenharmony_ci# 3462306a36Sopenharmony_ci# Multiple addresses can be specified on a single command line: 3562306a36Sopenharmony_ci# 3662306a36Sopenharmony_ci# $ ./scripts/faddr2line ~/k/vmlinux type_show+0x10/45 free_reserved_area+0x90 3762306a36Sopenharmony_ci# type_show+0x10/0x2d: 3862306a36Sopenharmony_ci# type_show at drivers/video/backlight/backlight.c:213 3962306a36Sopenharmony_ci# 4062306a36Sopenharmony_ci# free_reserved_area+0x90/0x123: 4162306a36Sopenharmony_ci# free_reserved_area at mm/page_alloc.c:6429 (discriminator 2) 4262306a36Sopenharmony_ci 4362306a36Sopenharmony_ci 4462306a36Sopenharmony_ciset -o errexit 4562306a36Sopenharmony_ciset -o nounset 4662306a36Sopenharmony_ci 4762306a36Sopenharmony_ciusage() { 4862306a36Sopenharmony_ci echo "usage: faddr2line [--list] <object file> <func+offset> <func+offset>..." >&2 4962306a36Sopenharmony_ci exit 1 5062306a36Sopenharmony_ci} 5162306a36Sopenharmony_ci 5262306a36Sopenharmony_ciwarn() { 5362306a36Sopenharmony_ci echo "$1" >&2 5462306a36Sopenharmony_ci} 5562306a36Sopenharmony_ci 5662306a36Sopenharmony_cidie() { 5762306a36Sopenharmony_ci echo "ERROR: $1" >&2 5862306a36Sopenharmony_ci exit 1 5962306a36Sopenharmony_ci} 6062306a36Sopenharmony_ci 6162306a36Sopenharmony_ciREADELF="${CROSS_COMPILE:-}readelf" 6262306a36Sopenharmony_ciADDR2LINE="${CROSS_COMPILE:-}addr2line" 6362306a36Sopenharmony_ciAWK="awk" 6462306a36Sopenharmony_ciGREP="grep" 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_cicommand -v ${AWK} >/dev/null 2>&1 || die "${AWK} isn't installed" 6762306a36Sopenharmony_cicommand -v ${READELF} >/dev/null 2>&1 || die "${READELF} isn't installed" 6862306a36Sopenharmony_cicommand -v ${ADDR2LINE} >/dev/null 2>&1 || die "${ADDR2LINE} isn't installed" 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_ci# Try to figure out the source directory prefix so we can remove it from the 7162306a36Sopenharmony_ci# addr2line output. HACK ALERT: This assumes that start_kernel() is in 7262306a36Sopenharmony_ci# init/main.c! This only works for vmlinux. Otherwise it falls back to 7362306a36Sopenharmony_ci# printing the absolute path. 7462306a36Sopenharmony_cifind_dir_prefix() { 7562306a36Sopenharmony_ci local objfile=$1 7662306a36Sopenharmony_ci 7762306a36Sopenharmony_ci local start_kernel_addr=$(${READELF} --symbols --wide $objfile | sed 's/\[.*\]//' | 7862306a36Sopenharmony_ci ${AWK} '$8 == "start_kernel" {printf "0x%s", $2}') 7962306a36Sopenharmony_ci [[ -z $start_kernel_addr ]] && return 8062306a36Sopenharmony_ci 8162306a36Sopenharmony_ci local file_line=$(${ADDR2LINE} -e $objfile $start_kernel_addr) 8262306a36Sopenharmony_ci [[ -z $file_line ]] && return 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_ci local prefix=${file_line%init/main.c:*} 8562306a36Sopenharmony_ci if [[ -z $prefix ]] || [[ $prefix = $file_line ]]; then 8662306a36Sopenharmony_ci return 8762306a36Sopenharmony_ci fi 8862306a36Sopenharmony_ci 8962306a36Sopenharmony_ci DIR_PREFIX=$prefix 9062306a36Sopenharmony_ci return 0 9162306a36Sopenharmony_ci} 9262306a36Sopenharmony_ci 9362306a36Sopenharmony_ci__faddr2line() { 9462306a36Sopenharmony_ci local objfile=$1 9562306a36Sopenharmony_ci local func_addr=$2 9662306a36Sopenharmony_ci local dir_prefix=$3 9762306a36Sopenharmony_ci local print_warnings=$4 9862306a36Sopenharmony_ci 9962306a36Sopenharmony_ci local sym_name=${func_addr%+*} 10062306a36Sopenharmony_ci local func_offset=${func_addr#*+} 10162306a36Sopenharmony_ci func_offset=${func_offset%/*} 10262306a36Sopenharmony_ci local user_size= 10362306a36Sopenharmony_ci local file_type 10462306a36Sopenharmony_ci local is_vmlinux=0 10562306a36Sopenharmony_ci [[ $func_addr =~ "/" ]] && user_size=${func_addr#*/} 10662306a36Sopenharmony_ci 10762306a36Sopenharmony_ci if [[ -z $sym_name ]] || [[ -z $func_offset ]] || [[ $sym_name = $func_addr ]]; then 10862306a36Sopenharmony_ci warn "bad func+offset $func_addr" 10962306a36Sopenharmony_ci DONE=1 11062306a36Sopenharmony_ci return 11162306a36Sopenharmony_ci fi 11262306a36Sopenharmony_ci 11362306a36Sopenharmony_ci # vmlinux uses absolute addresses in the section table rather than 11462306a36Sopenharmony_ci # section offsets. 11562306a36Sopenharmony_ci local file_type=$(${READELF} --file-header $objfile | 11662306a36Sopenharmony_ci ${AWK} '$1 == "Type:" { print $2; exit }') 11762306a36Sopenharmony_ci if [[ $file_type = "EXEC" ]] || [[ $file_type == "DYN" ]]; then 11862306a36Sopenharmony_ci is_vmlinux=1 11962306a36Sopenharmony_ci fi 12062306a36Sopenharmony_ci 12162306a36Sopenharmony_ci # Go through each of the object's symbols which match the func name. 12262306a36Sopenharmony_ci # In rare cases there might be duplicates, in which case we print all 12362306a36Sopenharmony_ci # matches. 12462306a36Sopenharmony_ci while read line; do 12562306a36Sopenharmony_ci local fields=($line) 12662306a36Sopenharmony_ci local sym_addr=0x${fields[1]} 12762306a36Sopenharmony_ci local sym_elf_size=${fields[2]} 12862306a36Sopenharmony_ci local sym_sec=${fields[6]} 12962306a36Sopenharmony_ci local sec_size 13062306a36Sopenharmony_ci local sec_name 13162306a36Sopenharmony_ci 13262306a36Sopenharmony_ci # Get the section size: 13362306a36Sopenharmony_ci sec_size=$(${READELF} --section-headers --wide $objfile | 13462306a36Sopenharmony_ci sed 's/\[ /\[/' | 13562306a36Sopenharmony_ci ${AWK} -v sec=$sym_sec '$1 == "[" sec "]" { print "0x" $6; exit }') 13662306a36Sopenharmony_ci 13762306a36Sopenharmony_ci if [[ -z $sec_size ]]; then 13862306a36Sopenharmony_ci warn "bad section size: section: $sym_sec" 13962306a36Sopenharmony_ci DONE=1 14062306a36Sopenharmony_ci return 14162306a36Sopenharmony_ci fi 14262306a36Sopenharmony_ci 14362306a36Sopenharmony_ci # Get the section name: 14462306a36Sopenharmony_ci sec_name=$(${READELF} --section-headers --wide $objfile | 14562306a36Sopenharmony_ci sed 's/\[ /\[/' | 14662306a36Sopenharmony_ci ${AWK} -v sec=$sym_sec '$1 == "[" sec "]" { print $2; exit }') 14762306a36Sopenharmony_ci 14862306a36Sopenharmony_ci if [[ -z $sec_name ]]; then 14962306a36Sopenharmony_ci warn "bad section name: section: $sym_sec" 15062306a36Sopenharmony_ci DONE=1 15162306a36Sopenharmony_ci return 15262306a36Sopenharmony_ci fi 15362306a36Sopenharmony_ci 15462306a36Sopenharmony_ci # Calculate the symbol size. 15562306a36Sopenharmony_ci # 15662306a36Sopenharmony_ci # Unfortunately we can't use the ELF size, because kallsyms 15762306a36Sopenharmony_ci # also includes the padding bytes in its size calculation. For 15862306a36Sopenharmony_ci # kallsyms, the size calculation is the distance between the 15962306a36Sopenharmony_ci # symbol and the next symbol in a sorted list. 16062306a36Sopenharmony_ci local sym_size 16162306a36Sopenharmony_ci local cur_sym_addr 16262306a36Sopenharmony_ci local found=0 16362306a36Sopenharmony_ci while read line; do 16462306a36Sopenharmony_ci local fields=($line) 16562306a36Sopenharmony_ci cur_sym_addr=0x${fields[1]} 16662306a36Sopenharmony_ci local cur_sym_elf_size=${fields[2]} 16762306a36Sopenharmony_ci local cur_sym_name=${fields[7]:-} 16862306a36Sopenharmony_ci 16962306a36Sopenharmony_ci if [[ $cur_sym_addr = $sym_addr ]] && 17062306a36Sopenharmony_ci [[ $cur_sym_elf_size = $sym_elf_size ]] && 17162306a36Sopenharmony_ci [[ $cur_sym_name = $sym_name ]]; then 17262306a36Sopenharmony_ci found=1 17362306a36Sopenharmony_ci continue 17462306a36Sopenharmony_ci fi 17562306a36Sopenharmony_ci 17662306a36Sopenharmony_ci if [[ $found = 1 ]]; then 17762306a36Sopenharmony_ci sym_size=$(($cur_sym_addr - $sym_addr)) 17862306a36Sopenharmony_ci [[ $sym_size -lt $sym_elf_size ]] && continue; 17962306a36Sopenharmony_ci found=2 18062306a36Sopenharmony_ci break 18162306a36Sopenharmony_ci fi 18262306a36Sopenharmony_ci done < <(${READELF} --symbols --wide $objfile | sed 's/\[.*\]//' | ${AWK} -v sec=$sym_sec '$7 == sec' | sort --key=2) 18362306a36Sopenharmony_ci 18462306a36Sopenharmony_ci if [[ $found = 0 ]]; then 18562306a36Sopenharmony_ci warn "can't find symbol: sym_name: $sym_name sym_sec: $sym_sec sym_addr: $sym_addr sym_elf_size: $sym_elf_size" 18662306a36Sopenharmony_ci DONE=1 18762306a36Sopenharmony_ci return 18862306a36Sopenharmony_ci fi 18962306a36Sopenharmony_ci 19062306a36Sopenharmony_ci # If nothing was found after the symbol, assume it's the last 19162306a36Sopenharmony_ci # symbol in the section. 19262306a36Sopenharmony_ci [[ $found = 1 ]] && sym_size=$(($sec_size - $sym_addr)) 19362306a36Sopenharmony_ci 19462306a36Sopenharmony_ci if [[ -z $sym_size ]] || [[ $sym_size -le 0 ]]; then 19562306a36Sopenharmony_ci warn "bad symbol size: sym_addr: $sym_addr cur_sym_addr: $cur_sym_addr" 19662306a36Sopenharmony_ci DONE=1 19762306a36Sopenharmony_ci return 19862306a36Sopenharmony_ci fi 19962306a36Sopenharmony_ci 20062306a36Sopenharmony_ci sym_size=0x$(printf %x $sym_size) 20162306a36Sopenharmony_ci 20262306a36Sopenharmony_ci # Calculate the address from user-supplied offset: 20362306a36Sopenharmony_ci local addr=$(($sym_addr + $func_offset)) 20462306a36Sopenharmony_ci if [[ -z $addr ]] || [[ $addr = 0 ]]; then 20562306a36Sopenharmony_ci warn "bad address: $sym_addr + $func_offset" 20662306a36Sopenharmony_ci DONE=1 20762306a36Sopenharmony_ci return 20862306a36Sopenharmony_ci fi 20962306a36Sopenharmony_ci addr=0x$(printf %x $addr) 21062306a36Sopenharmony_ci 21162306a36Sopenharmony_ci # If the user provided a size, make sure it matches the symbol's size: 21262306a36Sopenharmony_ci if [[ -n $user_size ]] && [[ $user_size -ne $sym_size ]]; then 21362306a36Sopenharmony_ci [[ $print_warnings = 1 ]] && 21462306a36Sopenharmony_ci echo "skipping $sym_name address at $addr due to size mismatch ($user_size != $sym_size)" 21562306a36Sopenharmony_ci continue; 21662306a36Sopenharmony_ci fi 21762306a36Sopenharmony_ci 21862306a36Sopenharmony_ci # Make sure the provided offset is within the symbol's range: 21962306a36Sopenharmony_ci if [[ $func_offset -gt $sym_size ]]; then 22062306a36Sopenharmony_ci [[ $print_warnings = 1 ]] && 22162306a36Sopenharmony_ci echo "skipping $sym_name address at $addr due to size mismatch ($func_offset > $sym_size)" 22262306a36Sopenharmony_ci continue 22362306a36Sopenharmony_ci fi 22462306a36Sopenharmony_ci 22562306a36Sopenharmony_ci # In case of duplicates or multiple addresses specified on the 22662306a36Sopenharmony_ci # cmdline, separate multiple entries with a blank line: 22762306a36Sopenharmony_ci [[ $FIRST = 0 ]] && echo 22862306a36Sopenharmony_ci FIRST=0 22962306a36Sopenharmony_ci 23062306a36Sopenharmony_ci echo "$sym_name+$func_offset/$sym_size:" 23162306a36Sopenharmony_ci 23262306a36Sopenharmony_ci # Pass section address to addr2line and strip absolute paths 23362306a36Sopenharmony_ci # from the output: 23462306a36Sopenharmony_ci local args="--functions --pretty-print --inlines --exe=$objfile" 23562306a36Sopenharmony_ci [[ $is_vmlinux = 0 ]] && args="$args --section=$sec_name" 23662306a36Sopenharmony_ci local output=$(${ADDR2LINE} $args $addr | sed "s; $dir_prefix\(\./\)*; ;") 23762306a36Sopenharmony_ci [[ -z $output ]] && continue 23862306a36Sopenharmony_ci 23962306a36Sopenharmony_ci # Default output (non --list): 24062306a36Sopenharmony_ci if [[ $LIST = 0 ]]; then 24162306a36Sopenharmony_ci echo "$output" | while read -r line 24262306a36Sopenharmony_ci do 24362306a36Sopenharmony_ci echo $line 24462306a36Sopenharmony_ci done 24562306a36Sopenharmony_ci DONE=1; 24662306a36Sopenharmony_ci continue 24762306a36Sopenharmony_ci fi 24862306a36Sopenharmony_ci 24962306a36Sopenharmony_ci # For --list, show each line with its corresponding source code: 25062306a36Sopenharmony_ci echo "$output" | while read -r line 25162306a36Sopenharmony_ci do 25262306a36Sopenharmony_ci echo 25362306a36Sopenharmony_ci echo $line 25462306a36Sopenharmony_ci n=$(echo $line | sed 's/.*:\([0-9]\+\).*/\1/g') 25562306a36Sopenharmony_ci n1=$[$n-5] 25662306a36Sopenharmony_ci n2=$[$n+5] 25762306a36Sopenharmony_ci f=$(echo $line | sed 's/.*at \(.\+\):.*/\1/g') 25862306a36Sopenharmony_ci ${AWK} 'NR>=strtonum("'$n1'") && NR<=strtonum("'$n2'") { if (NR=='$n') printf(">%d<", NR); else printf(" %d ", NR); printf("\t%s\n", $0)}' $f 25962306a36Sopenharmony_ci done 26062306a36Sopenharmony_ci 26162306a36Sopenharmony_ci DONE=1 26262306a36Sopenharmony_ci 26362306a36Sopenharmony_ci done < <(${READELF} --symbols --wide $objfile | sed 's/\[.*\]//' | ${AWK} -v fn=$sym_name '$4 == "FUNC" && $8 == fn') 26462306a36Sopenharmony_ci} 26562306a36Sopenharmony_ci 26662306a36Sopenharmony_ci[[ $# -lt 2 ]] && usage 26762306a36Sopenharmony_ci 26862306a36Sopenharmony_ciobjfile=$1 26962306a36Sopenharmony_ci 27062306a36Sopenharmony_ciLIST=0 27162306a36Sopenharmony_ci[[ "$objfile" == "--list" ]] && LIST=1 && shift && objfile=$1 27262306a36Sopenharmony_ci 27362306a36Sopenharmony_ci[[ ! -f $objfile ]] && die "can't find objfile $objfile" 27462306a36Sopenharmony_cishift 27562306a36Sopenharmony_ci 27662306a36Sopenharmony_ci${READELF} --section-headers --wide $objfile | ${GREP} -q '\.debug_info' || die "CONFIG_DEBUG_INFO not enabled" 27762306a36Sopenharmony_ci 27862306a36Sopenharmony_ciDIR_PREFIX=supercalifragilisticexpialidocious 27962306a36Sopenharmony_cifind_dir_prefix $objfile 28062306a36Sopenharmony_ci 28162306a36Sopenharmony_ciFIRST=1 28262306a36Sopenharmony_ciwhile [[ $# -gt 0 ]]; do 28362306a36Sopenharmony_ci func_addr=$1 28462306a36Sopenharmony_ci shift 28562306a36Sopenharmony_ci 28662306a36Sopenharmony_ci # print any matches found 28762306a36Sopenharmony_ci DONE=0 28862306a36Sopenharmony_ci __faddr2line $objfile $func_addr $DIR_PREFIX 0 28962306a36Sopenharmony_ci 29062306a36Sopenharmony_ci # if no match was found, print warnings 29162306a36Sopenharmony_ci if [[ $DONE = 0 ]]; then 29262306a36Sopenharmony_ci __faddr2line $objfile $func_addr $DIR_PREFIX 1 29362306a36Sopenharmony_ci warn "no match for $func_addr" 29462306a36Sopenharmony_ci fi 29562306a36Sopenharmony_cidone 296