167326666SJosh Poimboeuf#!/bin/bash 2b2441318SGreg Kroah-Hartman# SPDX-License-Identifier: GPL-2.0 367326666SJosh Poimboeuf# 467326666SJosh Poimboeuf# Translate stack dump function offsets. 567326666SJosh Poimboeuf# 667326666SJosh Poimboeuf# addr2line doesn't work with KASLR addresses. This works similarly to 767326666SJosh Poimboeuf# addr2line, but instead takes the 'func+0x123' format as input: 867326666SJosh Poimboeuf# 967326666SJosh Poimboeuf# $ ./scripts/faddr2line ~/k/vmlinux meminfo_proc_show+0x5/0x568 1067326666SJosh Poimboeuf# meminfo_proc_show+0x5/0x568: 1167326666SJosh Poimboeuf# meminfo_proc_show at fs/proc/meminfo.c:27 1267326666SJosh Poimboeuf# 1367326666SJosh Poimboeuf# If the address is part of an inlined function, the full inline call chain is 1467326666SJosh Poimboeuf# printed: 1567326666SJosh Poimboeuf# 1667326666SJosh Poimboeuf# $ ./scripts/faddr2line ~/k/vmlinux native_write_msr+0x6/0x27 1767326666SJosh Poimboeuf# native_write_msr+0x6/0x27: 1867326666SJosh Poimboeuf# arch_static_branch at arch/x86/include/asm/msr.h:121 1967326666SJosh Poimboeuf# (inlined by) static_key_false at include/linux/jump_label.h:125 2067326666SJosh Poimboeuf# (inlined by) native_write_msr at arch/x86/include/asm/msr.h:125 2167326666SJosh Poimboeuf# 2267326666SJosh Poimboeuf# The function size after the '/' in the input is optional, but recommended. 2367326666SJosh Poimboeuf# It's used to help disambiguate any duplicate symbol names, which can occur 2467326666SJosh Poimboeuf# rarely. If the size is omitted for a duplicate symbol then it's possible for 2567326666SJosh Poimboeuf# multiple code sites to be printed: 2667326666SJosh Poimboeuf# 2767326666SJosh Poimboeuf# $ ./scripts/faddr2line ~/k/vmlinux raw_ioctl+0x5 2867326666SJosh Poimboeuf# raw_ioctl+0x5/0x20: 2967326666SJosh Poimboeuf# raw_ioctl at drivers/char/raw.c:122 3067326666SJosh Poimboeuf# 3167326666SJosh Poimboeuf# raw_ioctl+0x5/0xb1: 3267326666SJosh Poimboeuf# raw_ioctl at net/ipv4/raw.c:876 3367326666SJosh Poimboeuf# 3467326666SJosh Poimboeuf# Multiple addresses can be specified on a single command line: 3567326666SJosh Poimboeuf# 3667326666SJosh Poimboeuf# $ ./scripts/faddr2line ~/k/vmlinux type_show+0x10/45 free_reserved_area+0x90 3767326666SJosh Poimboeuf# type_show+0x10/0x2d: 3867326666SJosh Poimboeuf# type_show at drivers/video/backlight/backlight.c:213 3967326666SJosh Poimboeuf# 4067326666SJosh Poimboeuf# free_reserved_area+0x90/0x123: 4167326666SJosh Poimboeuf# free_reserved_area at mm/page_alloc.c:6429 (discriminator 2) 4267326666SJosh Poimboeuf 4367326666SJosh Poimboeuf 4467326666SJosh Poimboeufset -o errexit 4567326666SJosh Poimboeufset -o nounset 4667326666SJosh Poimboeuf 4767326666SJosh Poimboeufusage() { 48689135f0SPeter Zijlstra (Intel) echo "usage: faddr2line [--list] <object file> <func+offset> <func+offset>..." >&2 4967326666SJosh Poimboeuf exit 1 5067326666SJosh Poimboeuf} 5167326666SJosh Poimboeuf 5267326666SJosh Poimboeufwarn() { 5367326666SJosh Poimboeuf echo "$1" >&2 5467326666SJosh Poimboeuf} 5567326666SJosh Poimboeuf 5667326666SJosh Poimboeufdie() { 5767326666SJosh Poimboeuf echo "ERROR: $1" >&2 5867326666SJosh Poimboeuf exit 1 5967326666SJosh Poimboeuf} 6067326666SJosh Poimboeuf 611d1a0e7cSJosh PoimboeufREADELF="${CROSS_COMPILE:-}readelf" 621d1a0e7cSJosh PoimboeufADDR2LINE="${CROSS_COMPILE:-}addr2line" 631d1a0e7cSJosh PoimboeufAWK="awk" 64a41a2e2eSJosh PoimboeufGREP="grep" 651d1a0e7cSJosh Poimboeuf 661d1a0e7cSJosh Poimboeufcommand -v ${AWK} >/dev/null 2>&1 || die "${AWK} isn't installed" 671d1a0e7cSJosh Poimboeufcommand -v ${READELF} >/dev/null 2>&1 || die "${READELF} isn't installed" 681d1a0e7cSJosh Poimboeufcommand -v ${ADDR2LINE} >/dev/null 2>&1 || die "${ADDR2LINE} isn't installed" 691d1a0e7cSJosh Poimboeuf 7067326666SJosh Poimboeuf# Try to figure out the source directory prefix so we can remove it from the 7167326666SJosh Poimboeuf# addr2line output. HACK ALERT: This assumes that start_kernel() is in 72f5f67cc0SRandy Dunlap# init/main.c! This only works for vmlinux. Otherwise it falls back to 7367326666SJosh Poimboeuf# printing the absolute path. 7467326666SJosh Poimboeuffind_dir_prefix() { 7567326666SJosh Poimboeuf local objfile=$1 7667326666SJosh Poimboeuf 77*2d77de15SSrikar Dronamraju local start_kernel_addr=$(${READELF} --symbols --wide $objfile | sed 's/\[.*\]//' | 78*2d77de15SSrikar Dronamraju ${AWK} '$8 == "start_kernel" {printf "0x%s", $2}') 7967326666SJosh Poimboeuf [[ -z $start_kernel_addr ]] && return 8067326666SJosh Poimboeuf 8195a87982SLiu, Changcheng local file_line=$(${ADDR2LINE} -e $objfile $start_kernel_addr) 8267326666SJosh Poimboeuf [[ -z $file_line ]] && return 8367326666SJosh Poimboeuf 8467326666SJosh Poimboeuf local prefix=${file_line%init/main.c:*} 8567326666SJosh Poimboeuf if [[ -z $prefix ]] || [[ $prefix = $file_line ]]; then 8667326666SJosh Poimboeuf return 8767326666SJosh Poimboeuf fi 8867326666SJosh Poimboeuf 8967326666SJosh Poimboeuf DIR_PREFIX=$prefix 9067326666SJosh Poimboeuf return 0 9167326666SJosh Poimboeuf} 9267326666SJosh Poimboeuf 9367326666SJosh Poimboeuf__faddr2line() { 9467326666SJosh Poimboeuf local objfile=$1 9567326666SJosh Poimboeuf local func_addr=$2 9667326666SJosh Poimboeuf local dir_prefix=$3 9767326666SJosh Poimboeuf local print_warnings=$4 9867326666SJosh Poimboeuf 991d1a0e7cSJosh Poimboeuf local sym_name=${func_addr%+*} 100dcea997bSJosh Poimboeuf local func_offset=${func_addr#*+} 101dcea997bSJosh Poimboeuf func_offset=${func_offset%/*} 1021d1a0e7cSJosh Poimboeuf local user_size= 103dcea997bSJosh Poimboeuf local file_type 104dcea997bSJosh Poimboeuf local is_vmlinux=0 1051d1a0e7cSJosh Poimboeuf [[ $func_addr =~ "/" ]] && user_size=${func_addr#*/} 10667326666SJosh Poimboeuf 107dcea997bSJosh Poimboeuf if [[ -z $sym_name ]] || [[ -z $func_offset ]] || [[ $sym_name = $func_addr ]]; then 10867326666SJosh Poimboeuf warn "bad func+offset $func_addr" 10967326666SJosh Poimboeuf DONE=1 11067326666SJosh Poimboeuf return 11167326666SJosh Poimboeuf fi 11267326666SJosh Poimboeuf 113dcea997bSJosh Poimboeuf # vmlinux uses absolute addresses in the section table rather than 114dcea997bSJosh Poimboeuf # section offsets. 115dcea997bSJosh Poimboeuf local file_type=$(${READELF} --file-header $objfile | 116dcea997bSJosh Poimboeuf ${AWK} '$1 == "Type:" { print $2; exit }') 117b6a50688SJosh Poimboeuf if [[ $file_type = "EXEC" ]] || [[ $file_type == "DYN" ]]; then 118b6a50688SJosh Poimboeuf is_vmlinux=1 119b6a50688SJosh Poimboeuf fi 120dcea997bSJosh Poimboeuf 12167326666SJosh Poimboeuf # Go through each of the object's symbols which match the func name. 1221d1a0e7cSJosh Poimboeuf # In rare cases there might be duplicates, in which case we print all 1231d1a0e7cSJosh Poimboeuf # matches. 1241d1a0e7cSJosh Poimboeuf while read line; do 1251d1a0e7cSJosh Poimboeuf local fields=($line) 1261d1a0e7cSJosh Poimboeuf local sym_addr=0x${fields[1]} 1271d1a0e7cSJosh Poimboeuf local sym_elf_size=${fields[2]} 1281d1a0e7cSJosh Poimboeuf local sym_sec=${fields[6]} 129dcea997bSJosh Poimboeuf local sec_size 130dcea997bSJosh Poimboeuf local sec_name 131efdb4167SJosh Poimboeuf 1321d1a0e7cSJosh Poimboeuf # Get the section size: 133dcea997bSJosh Poimboeuf sec_size=$(${READELF} --section-headers --wide $objfile | 1341d1a0e7cSJosh Poimboeuf sed 's/\[ /\[/' | 1351d1a0e7cSJosh Poimboeuf ${AWK} -v sec=$sym_sec '$1 == "[" sec "]" { print "0x" $6; exit }') 1361d1a0e7cSJosh Poimboeuf 1371d1a0e7cSJosh Poimboeuf if [[ -z $sec_size ]]; then 1381d1a0e7cSJosh Poimboeuf warn "bad section size: section: $sym_sec" 139efdb4167SJosh Poimboeuf DONE=1 140efdb4167SJosh Poimboeuf return 141efdb4167SJosh Poimboeuf fi 1421d1a0e7cSJosh Poimboeuf 143dcea997bSJosh Poimboeuf # Get the section name: 144dcea997bSJosh Poimboeuf sec_name=$(${READELF} --section-headers --wide $objfile | 145dcea997bSJosh Poimboeuf sed 's/\[ /\[/' | 146dcea997bSJosh Poimboeuf ${AWK} -v sec=$sym_sec '$1 == "[" sec "]" { print $2; exit }') 147dcea997bSJosh Poimboeuf 148dcea997bSJosh Poimboeuf if [[ -z $sec_name ]]; then 149dcea997bSJosh Poimboeuf warn "bad section name: section: $sym_sec" 150dcea997bSJosh Poimboeuf DONE=1 151dcea997bSJosh Poimboeuf return 152dcea997bSJosh Poimboeuf fi 153dcea997bSJosh Poimboeuf 1541d1a0e7cSJosh Poimboeuf # Calculate the symbol size. 1551d1a0e7cSJosh Poimboeuf # 1561d1a0e7cSJosh Poimboeuf # Unfortunately we can't use the ELF size, because kallsyms 1571d1a0e7cSJosh Poimboeuf # also includes the padding bytes in its size calculation. For 1581d1a0e7cSJosh Poimboeuf # kallsyms, the size calculation is the distance between the 1591d1a0e7cSJosh Poimboeuf # symbol and the next symbol in a sorted list. 1601d1a0e7cSJosh Poimboeuf local sym_size 1611d1a0e7cSJosh Poimboeuf local cur_sym_addr 1621d1a0e7cSJosh Poimboeuf local found=0 1631d1a0e7cSJosh Poimboeuf while read line; do 1641d1a0e7cSJosh Poimboeuf local fields=($line) 1651d1a0e7cSJosh Poimboeuf cur_sym_addr=0x${fields[1]} 1661d1a0e7cSJosh Poimboeuf local cur_sym_elf_size=${fields[2]} 1671d1a0e7cSJosh Poimboeuf local cur_sym_name=${fields[7]:-} 1681d1a0e7cSJosh Poimboeuf 1691d1a0e7cSJosh Poimboeuf if [[ $cur_sym_addr = $sym_addr ]] && 1701d1a0e7cSJosh Poimboeuf [[ $cur_sym_elf_size = $sym_elf_size ]] && 1711d1a0e7cSJosh Poimboeuf [[ $cur_sym_name = $sym_name ]]; then 1721d1a0e7cSJosh Poimboeuf found=1 1731d1a0e7cSJosh Poimboeuf continue 1741d1a0e7cSJosh Poimboeuf fi 1751d1a0e7cSJosh Poimboeuf 1761d1a0e7cSJosh Poimboeuf if [[ $found = 1 ]]; then 1771d1a0e7cSJosh Poimboeuf sym_size=$(($cur_sym_addr - $sym_addr)) 1781d1a0e7cSJosh Poimboeuf [[ $sym_size -lt $sym_elf_size ]] && continue; 1791d1a0e7cSJosh Poimboeuf found=2 1801d1a0e7cSJosh Poimboeuf break 1811d1a0e7cSJosh Poimboeuf fi 182*2d77de15SSrikar Dronamraju done < <(${READELF} --symbols --wide $objfile | sed 's/\[.*\]//' | ${AWK} -v sec=$sym_sec '$7 == sec' | sort --key=2) 1831d1a0e7cSJosh Poimboeuf 1841d1a0e7cSJosh Poimboeuf if [[ $found = 0 ]]; then 1851d1a0e7cSJosh Poimboeuf warn "can't find symbol: sym_name: $sym_name sym_sec: $sym_sec sym_addr: $sym_addr sym_elf_size: $sym_elf_size" 1861d1a0e7cSJosh Poimboeuf DONE=1 1871d1a0e7cSJosh Poimboeuf return 1881d1a0e7cSJosh Poimboeuf fi 1891d1a0e7cSJosh Poimboeuf 1901d1a0e7cSJosh Poimboeuf # If nothing was found after the symbol, assume it's the last 1911d1a0e7cSJosh Poimboeuf # symbol in the section. 1921d1a0e7cSJosh Poimboeuf [[ $found = 1 ]] && sym_size=$(($sec_size - $sym_addr)) 1931d1a0e7cSJosh Poimboeuf 1941d1a0e7cSJosh Poimboeuf if [[ -z $sym_size ]] || [[ $sym_size -le 0 ]]; then 1951d1a0e7cSJosh Poimboeuf warn "bad symbol size: sym_addr: $sym_addr cur_sym_addr: $cur_sym_addr" 1961d1a0e7cSJosh Poimboeuf DONE=1 1971d1a0e7cSJosh Poimboeuf return 1981d1a0e7cSJosh Poimboeuf fi 1991d1a0e7cSJosh Poimboeuf 200efdb4167SJosh Poimboeuf sym_size=0x$(printf %x $sym_size) 20167326666SJosh Poimboeuf 202dcea997bSJosh Poimboeuf # Calculate the address from user-supplied offset: 203dcea997bSJosh Poimboeuf local addr=$(($sym_addr + $func_offset)) 20467326666SJosh Poimboeuf if [[ -z $addr ]] || [[ $addr = 0 ]]; then 205dcea997bSJosh Poimboeuf warn "bad address: $sym_addr + $func_offset" 20667326666SJosh Poimboeuf DONE=1 20767326666SJosh Poimboeuf return 20867326666SJosh Poimboeuf fi 209efdb4167SJosh Poimboeuf addr=0x$(printf %x $addr) 21067326666SJosh Poimboeuf 2111d1a0e7cSJosh Poimboeuf # If the user provided a size, make sure it matches the symbol's size: 2121d1a0e7cSJosh Poimboeuf if [[ -n $user_size ]] && [[ $user_size -ne $sym_size ]]; then 21367326666SJosh Poimboeuf [[ $print_warnings = 1 ]] && 2141d1a0e7cSJosh Poimboeuf echo "skipping $sym_name address at $addr due to size mismatch ($user_size != $sym_size)" 21567326666SJosh Poimboeuf continue; 21667326666SJosh Poimboeuf fi 21767326666SJosh Poimboeuf 2181d1a0e7cSJosh Poimboeuf # Make sure the provided offset is within the symbol's range: 219dcea997bSJosh Poimboeuf if [[ $func_offset -gt $sym_size ]]; then 22067326666SJosh Poimboeuf [[ $print_warnings = 1 ]] && 221dcea997bSJosh Poimboeuf echo "skipping $sym_name address at $addr due to size mismatch ($func_offset > $sym_size)" 22267326666SJosh Poimboeuf continue 22367326666SJosh Poimboeuf fi 22467326666SJosh Poimboeuf 2251d1a0e7cSJosh Poimboeuf # In case of duplicates or multiple addresses specified on the 2261d1a0e7cSJosh Poimboeuf # cmdline, separate multiple entries with a blank line: 22767326666SJosh Poimboeuf [[ $FIRST = 0 ]] && echo 22867326666SJosh Poimboeuf FIRST=0 22967326666SJosh Poimboeuf 230dcea997bSJosh Poimboeuf echo "$sym_name+$func_offset/$sym_size:" 2316870c016SChangbin Du 2321d1a0e7cSJosh Poimboeuf # Pass section address to addr2line and strip absolute paths 2331d1a0e7cSJosh Poimboeuf # from the output: 234dcea997bSJosh Poimboeuf local args="--functions --pretty-print --inlines --exe=$objfile" 235dcea997bSJosh Poimboeuf [[ $is_vmlinux = 0 ]] && args="$args --section=$sec_name" 236dcea997bSJosh Poimboeuf local output=$(${ADDR2LINE} $args $addr | sed "s; $dir_prefix\(\./\)*; ;") 2371d1a0e7cSJosh Poimboeuf [[ -z $output ]] && continue 2381d1a0e7cSJosh Poimboeuf 2391d1a0e7cSJosh Poimboeuf # Default output (non --list): 240689135f0SPeter Zijlstra (Intel) if [[ $LIST = 0 ]]; then 2411d1a0e7cSJosh Poimboeuf echo "$output" | while read -r line 242689135f0SPeter Zijlstra (Intel) do 243689135f0SPeter Zijlstra (Intel) echo $line 244689135f0SPeter Zijlstra (Intel) done 245689135f0SPeter Zijlstra (Intel) DONE=1; 2461d1a0e7cSJosh Poimboeuf continue 247689135f0SPeter Zijlstra (Intel) fi 248689135f0SPeter Zijlstra (Intel) 2491d1a0e7cSJosh Poimboeuf # For --list, show each line with its corresponding source code: 2501d1a0e7cSJosh Poimboeuf echo "$output" | while read -r line 2516870c016SChangbin Du do 252689135f0SPeter Zijlstra (Intel) echo 2536870c016SChangbin Du echo $line 25478eb0c63SChangbin Du n=$(echo $line | sed 's/.*:\([0-9]\+\).*/\1/g') 25578eb0c63SChangbin Du n1=$[$n-5] 25678eb0c63SChangbin Du n2=$[$n+5] 25778eb0c63SChangbin Du f=$(echo $line | sed 's/.*at \(.\+\):.*/\1/g') 2581d1a0e7cSJosh Poimboeuf ${AWK} 'NR>=strtonum("'$n1'") && NR<=strtonum("'$n2'") { if (NR=='$n') printf(">%d<", NR); else printf(" %d ", NR); printf("\t%s\n", $0)}' $f 2596870c016SChangbin Du done 2606870c016SChangbin Du 26167326666SJosh Poimboeuf DONE=1 26267326666SJosh Poimboeuf 263*2d77de15SSrikar Dronamraju done < <(${READELF} --symbols --wide $objfile | sed 's/\[.*\]//' | ${AWK} -v fn=$sym_name '$4 == "FUNC" && $8 == fn') 26467326666SJosh Poimboeuf} 26567326666SJosh Poimboeuf 26667326666SJosh Poimboeuf[[ $# -lt 2 ]] && usage 26767326666SJosh Poimboeuf 26867326666SJosh Poimboeufobjfile=$1 269689135f0SPeter Zijlstra (Intel) 270689135f0SPeter Zijlstra (Intel)LIST=0 271689135f0SPeter Zijlstra (Intel)[[ "$objfile" == "--list" ]] && LIST=1 && shift && objfile=$1 272689135f0SPeter Zijlstra (Intel) 27367326666SJosh Poimboeuf[[ ! -f $objfile ]] && die "can't find objfile $objfile" 27467326666SJosh Poimboeufshift 27567326666SJosh Poimboeuf 276a41a2e2eSJosh Poimboeuf${READELF} --section-headers --wide $objfile | ${GREP} -q '\.debug_info' || die "CONFIG_DEBUG_INFO not enabled" 277a41a2e2eSJosh Poimboeuf 27867326666SJosh PoimboeufDIR_PREFIX=supercalifragilisticexpialidocious 27967326666SJosh Poimboeuffind_dir_prefix $objfile 28067326666SJosh Poimboeuf 28167326666SJosh PoimboeufFIRST=1 28267326666SJosh Poimboeufwhile [[ $# -gt 0 ]]; do 28367326666SJosh Poimboeuf func_addr=$1 28467326666SJosh Poimboeuf shift 28567326666SJosh Poimboeuf 28667326666SJosh Poimboeuf # print any matches found 28767326666SJosh Poimboeuf DONE=0 28867326666SJosh Poimboeuf __faddr2line $objfile $func_addr $DIR_PREFIX 0 28967326666SJosh Poimboeuf 29067326666SJosh Poimboeuf # if no match was found, print warnings 29167326666SJosh Poimboeuf if [[ $DONE = 0 ]]; then 29267326666SJosh Poimboeuf __faddr2line $objfile $func_addr $DIR_PREFIX 1 29367326666SJosh Poimboeuf warn "no match for $func_addr" 29467326666SJosh Poimboeuf fi 29567326666SJosh Poimboeufdone 296