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 474cc90b4cSLiu, ChangchengREADELF="${CROSS_COMPILE:-}readelf" 484cc90b4cSLiu, ChangchengADDR2LINE="${CROSS_COMPILE:-}addr2line" 494cc90b4cSLiu, ChangchengSIZE="${CROSS_COMPILE:-}size" 504cc90b4cSLiu, ChangchengNM="${CROSS_COMPILE:-}nm" 5195a87982SLiu, Changcheng 5267326666SJosh Poimboeufcommand -v awk >/dev/null 2>&1 || die "awk isn't installed" 5395a87982SLiu, Changchengcommand -v ${READELF} >/dev/null 2>&1 || die "readelf isn't installed" 5495a87982SLiu, Changchengcommand -v ${ADDR2LINE} >/dev/null 2>&1 || die "addr2line isn't installed" 5595a87982SLiu, Changchengcommand -v ${SIZE} >/dev/null 2>&1 || die "size isn't installed" 5695a87982SLiu, Changchengcommand -v ${NM} >/dev/null 2>&1 || die "nm isn't installed" 5767326666SJosh Poimboeuf 5867326666SJosh Poimboeufusage() { 59689135f0SPeter Zijlstra (Intel) echo "usage: faddr2line [--list] <object file> <func+offset> <func+offset>..." >&2 6067326666SJosh Poimboeuf exit 1 6167326666SJosh Poimboeuf} 6267326666SJosh Poimboeuf 6367326666SJosh Poimboeufwarn() { 6467326666SJosh Poimboeuf echo "$1" >&2 6567326666SJosh Poimboeuf} 6667326666SJosh Poimboeuf 6767326666SJosh Poimboeufdie() { 6867326666SJosh Poimboeuf echo "ERROR: $1" >&2 6967326666SJosh Poimboeuf exit 1 7067326666SJosh Poimboeuf} 7167326666SJosh Poimboeuf 7267326666SJosh Poimboeuf# Try to figure out the source directory prefix so we can remove it from the 7367326666SJosh Poimboeuf# addr2line output. HACK ALERT: This assumes that start_kernel() is in 74*f5f67cc0SRandy Dunlap# init/main.c! This only works for vmlinux. Otherwise it falls back to 7567326666SJosh Poimboeuf# printing the absolute path. 7667326666SJosh Poimboeuffind_dir_prefix() { 7767326666SJosh Poimboeuf local objfile=$1 7867326666SJosh Poimboeuf 7995a87982SLiu, Changcheng local start_kernel_addr=$(${READELF} -sW $objfile | awk '$8 == "start_kernel" {printf "0x%s", $2}') 8067326666SJosh Poimboeuf [[ -z $start_kernel_addr ]] && return 8167326666SJosh Poimboeuf 8295a87982SLiu, Changcheng local file_line=$(${ADDR2LINE} -e $objfile $start_kernel_addr) 8367326666SJosh Poimboeuf [[ -z $file_line ]] && return 8467326666SJosh Poimboeuf 8567326666SJosh Poimboeuf local prefix=${file_line%init/main.c:*} 8667326666SJosh Poimboeuf if [[ -z $prefix ]] || [[ $prefix = $file_line ]]; then 8767326666SJosh Poimboeuf return 8867326666SJosh Poimboeuf fi 8967326666SJosh Poimboeuf 9067326666SJosh Poimboeuf DIR_PREFIX=$prefix 9167326666SJosh Poimboeuf return 0 9267326666SJosh Poimboeuf} 9367326666SJosh Poimboeuf 9467326666SJosh Poimboeuf__faddr2line() { 9567326666SJosh Poimboeuf local objfile=$1 9667326666SJosh Poimboeuf local func_addr=$2 9767326666SJosh Poimboeuf local dir_prefix=$3 9867326666SJosh Poimboeuf local print_warnings=$4 9967326666SJosh Poimboeuf 10067326666SJosh Poimboeuf local func=${func_addr%+*} 10167326666SJosh Poimboeuf local offset=${func_addr#*+} 10267326666SJosh Poimboeuf offset=${offset%/*} 10367326666SJosh Poimboeuf local size= 10467326666SJosh Poimboeuf [[ $func_addr =~ "/" ]] && size=${func_addr#*/} 10567326666SJosh Poimboeuf 10667326666SJosh Poimboeuf if [[ -z $func ]] || [[ -z $offset ]] || [[ $func = $func_addr ]]; then 10767326666SJosh Poimboeuf warn "bad func+offset $func_addr" 10867326666SJosh Poimboeuf DONE=1 10967326666SJosh Poimboeuf return 11067326666SJosh Poimboeuf fi 11167326666SJosh Poimboeuf 11267326666SJosh Poimboeuf # Go through each of the object's symbols which match the func name. 11367326666SJosh Poimboeuf # In rare cases there might be duplicates. 11495a87982SLiu, Changcheng file_end=$(${SIZE} -Ax $objfile | awk '$1 == ".text" {print $2}') 11567326666SJosh Poimboeuf while read symbol; do 11667326666SJosh Poimboeuf local fields=($symbol) 117efdb4167SJosh Poimboeuf local sym_base=0x${fields[0]} 118efdb4167SJosh Poimboeuf local sym_type=${fields[1]} 1192aab9c3cSNeilBrown local sym_end=${fields[3]} 120efdb4167SJosh Poimboeuf 121efdb4167SJosh Poimboeuf # calculate the size 122efdb4167SJosh Poimboeuf local sym_size=$(($sym_end - $sym_base)) 123efdb4167SJosh Poimboeuf if [[ -z $sym_size ]] || [[ $sym_size -le 0 ]]; then 124efdb4167SJosh Poimboeuf warn "bad symbol size: base: $sym_base end: $sym_end" 125efdb4167SJosh Poimboeuf DONE=1 126efdb4167SJosh Poimboeuf return 127efdb4167SJosh Poimboeuf fi 128efdb4167SJosh Poimboeuf sym_size=0x$(printf %x $sym_size) 12967326666SJosh Poimboeuf 13067326666SJosh Poimboeuf # calculate the address 13167326666SJosh Poimboeuf local addr=$(($sym_base + $offset)) 13267326666SJosh Poimboeuf if [[ -z $addr ]] || [[ $addr = 0 ]]; then 13367326666SJosh Poimboeuf warn "bad address: $sym_base + $offset" 13467326666SJosh Poimboeuf DONE=1 13567326666SJosh Poimboeuf return 13667326666SJosh Poimboeuf fi 137efdb4167SJosh Poimboeuf addr=0x$(printf %x $addr) 13867326666SJosh Poimboeuf 13967326666SJosh Poimboeuf # weed out non-function symbols 140efdb4167SJosh Poimboeuf if [[ $sym_type != t ]] && [[ $sym_type != T ]]; then 14167326666SJosh Poimboeuf [[ $print_warnings = 1 ]] && 142efdb4167SJosh Poimboeuf echo "skipping $func address at $addr due to non-function symbol of type '$sym_type'" 14367326666SJosh Poimboeuf continue 14467326666SJosh Poimboeuf fi 14567326666SJosh Poimboeuf 14667326666SJosh Poimboeuf # if the user provided a size, make sure it matches the symbol's size 14767326666SJosh Poimboeuf if [[ -n $size ]] && [[ $size -ne $sym_size ]]; then 14867326666SJosh Poimboeuf [[ $print_warnings = 1 ]] && 149efdb4167SJosh Poimboeuf echo "skipping $func address at $addr due to size mismatch ($size != $sym_size)" 15067326666SJosh Poimboeuf continue; 15167326666SJosh Poimboeuf fi 15267326666SJosh Poimboeuf 15367326666SJosh Poimboeuf # make sure the provided offset is within the symbol's range 15467326666SJosh Poimboeuf if [[ $offset -gt $sym_size ]]; then 15567326666SJosh Poimboeuf [[ $print_warnings = 1 ]] && 156efdb4167SJosh Poimboeuf echo "skipping $func address at $addr due to size mismatch ($offset > $sym_size)" 15767326666SJosh Poimboeuf continue 15867326666SJosh Poimboeuf fi 15967326666SJosh Poimboeuf 16067326666SJosh Poimboeuf # separate multiple entries with a blank line 16167326666SJosh Poimboeuf [[ $FIRST = 0 ]] && echo 16267326666SJosh Poimboeuf FIRST=0 16367326666SJosh Poimboeuf 164efdb4167SJosh Poimboeuf # pass real address to addr2line 165efdb4167SJosh Poimboeuf echo "$func+$offset/$sym_size:" 1666870c016SChangbin Du local file_lines=$(${ADDR2LINE} -fpie $objfile $addr | sed "s; $dir_prefix\(\./\)*; ;") 1676870c016SChangbin Du [[ -z $file_lines ]] && return 1686870c016SChangbin Du 169689135f0SPeter Zijlstra (Intel) if [[ $LIST = 0 ]]; then 170689135f0SPeter Zijlstra (Intel) echo "$file_lines" | while read -r line 171689135f0SPeter Zijlstra (Intel) do 172689135f0SPeter Zijlstra (Intel) echo $line 173689135f0SPeter Zijlstra (Intel) done 174689135f0SPeter Zijlstra (Intel) DONE=1; 175689135f0SPeter Zijlstra (Intel) return 176689135f0SPeter Zijlstra (Intel) fi 177689135f0SPeter Zijlstra (Intel) 1786870c016SChangbin Du # show each line with context 1796870c016SChangbin Du echo "$file_lines" | while read -r line 1806870c016SChangbin Du do 181689135f0SPeter Zijlstra (Intel) echo 1826870c016SChangbin Du echo $line 18378eb0c63SChangbin Du n=$(echo $line | sed 's/.*:\([0-9]\+\).*/\1/g') 18478eb0c63SChangbin Du n1=$[$n-5] 18578eb0c63SChangbin Du n2=$[$n+5] 18678eb0c63SChangbin Du f=$(echo $line | sed 's/.*at \(.\+\):.*/\1/g') 187689135f0SPeter Zijlstra (Intel) awk 'NR>=strtonum("'$n1'") && NR<=strtonum("'$n2'") { if (NR=='$n') printf(">%d<", NR); else printf(" %d ", NR); printf("\t%s\n", $0)}' $f 1886870c016SChangbin Du done 1896870c016SChangbin Du 19067326666SJosh Poimboeuf DONE=1 19167326666SJosh Poimboeuf 19295a87982SLiu, Changcheng done < <(${NM} -n $objfile | awk -v fn=$func -v end=$file_end '$3 == fn { found=1; line=$0; start=$1; next } found == 1 { found=0; print line, "0x"$1 } END {if (found == 1) print line, end; }') 19367326666SJosh Poimboeuf} 19467326666SJosh Poimboeuf 19567326666SJosh Poimboeuf[[ $# -lt 2 ]] && usage 19667326666SJosh Poimboeuf 19767326666SJosh Poimboeufobjfile=$1 198689135f0SPeter Zijlstra (Intel) 199689135f0SPeter Zijlstra (Intel)LIST=0 200689135f0SPeter Zijlstra (Intel)[[ "$objfile" == "--list" ]] && LIST=1 && shift && objfile=$1 201689135f0SPeter Zijlstra (Intel) 20267326666SJosh Poimboeuf[[ ! -f $objfile ]] && die "can't find objfile $objfile" 20367326666SJosh Poimboeufshift 20467326666SJosh Poimboeuf 20567326666SJosh PoimboeufDIR_PREFIX=supercalifragilisticexpialidocious 20667326666SJosh Poimboeuffind_dir_prefix $objfile 20767326666SJosh Poimboeuf 20867326666SJosh PoimboeufFIRST=1 20967326666SJosh Poimboeufwhile [[ $# -gt 0 ]]; do 21067326666SJosh Poimboeuf func_addr=$1 21167326666SJosh Poimboeuf shift 21267326666SJosh Poimboeuf 21367326666SJosh Poimboeuf # print any matches found 21467326666SJosh Poimboeuf DONE=0 21567326666SJosh Poimboeuf __faddr2line $objfile $func_addr $DIR_PREFIX 0 21667326666SJosh Poimboeuf 21767326666SJosh Poimboeuf # if no match was found, print warnings 21867326666SJosh Poimboeuf if [[ $DONE = 0 ]]; then 21967326666SJosh Poimboeuf __faddr2line $objfile $func_addr $DIR_PREFIX 1 22067326666SJosh Poimboeuf warn "no match for $func_addr" 22167326666SJosh Poimboeuf fi 22267326666SJosh Poimboeufdone 223