Back to home page

LXR

 
 

    


0001 #!/bin/bash
0002 #
0003 # Translate stack dump function offsets.
0004 #
0005 # addr2line doesn't work with KASLR addresses.  This works similarly to
0006 # addr2line, but instead takes the 'func+0x123' format as input:
0007 #
0008 #   $ ./scripts/faddr2line ~/k/vmlinux meminfo_proc_show+0x5/0x568
0009 #   meminfo_proc_show+0x5/0x568:
0010 #   meminfo_proc_show at fs/proc/meminfo.c:27
0011 #
0012 # If the address is part of an inlined function, the full inline call chain is
0013 # printed:
0014 #
0015 #   $ ./scripts/faddr2line ~/k/vmlinux native_write_msr+0x6/0x27
0016 #   native_write_msr+0x6/0x27:
0017 #   arch_static_branch at arch/x86/include/asm/msr.h:121
0018 #    (inlined by) static_key_false at include/linux/jump_label.h:125
0019 #    (inlined by) native_write_msr at arch/x86/include/asm/msr.h:125
0020 #
0021 # The function size after the '/' in the input is optional, but recommended.
0022 # It's used to help disambiguate any duplicate symbol names, which can occur
0023 # rarely.  If the size is omitted for a duplicate symbol then it's possible for
0024 # multiple code sites to be printed:
0025 #
0026 #   $ ./scripts/faddr2line ~/k/vmlinux raw_ioctl+0x5
0027 #   raw_ioctl+0x5/0x20:
0028 #   raw_ioctl at drivers/char/raw.c:122
0029 #
0030 #   raw_ioctl+0x5/0xb1:
0031 #   raw_ioctl at net/ipv4/raw.c:876
0032 #
0033 # Multiple addresses can be specified on a single command line:
0034 #
0035 #   $ ./scripts/faddr2line ~/k/vmlinux type_show+0x10/45 free_reserved_area+0x90
0036 #   type_show+0x10/0x2d:
0037 #   type_show at drivers/video/backlight/backlight.c:213
0038 #
0039 #   free_reserved_area+0x90/0x123:
0040 #   free_reserved_area at mm/page_alloc.c:6429 (discriminator 2)
0041 
0042 
0043 set -o errexit
0044 set -o nounset
0045 
0046 command -v awk >/dev/null 2>&1 || die "awk isn't installed"
0047 command -v readelf >/dev/null 2>&1 || die "readelf isn't installed"
0048 command -v addr2line >/dev/null 2>&1 || die "addr2line isn't installed"
0049 
0050 usage() {
0051         echo "usage: faddr2line <object file> <func+offset> <func+offset>..." >&2
0052         exit 1
0053 }
0054 
0055 warn() {
0056         echo "$1" >&2
0057 }
0058 
0059 die() {
0060         echo "ERROR: $1" >&2
0061         exit 1
0062 }
0063 
0064 # Try to figure out the source directory prefix so we can remove it from the
0065 # addr2line output.  HACK ALERT: This assumes that start_kernel() is in
0066 # kernel/init.c!  This only works for vmlinux.  Otherwise it falls back to
0067 # printing the absolute path.
0068 find_dir_prefix() {
0069         local objfile=$1
0070 
0071         local start_kernel_addr=$(readelf -sW $objfile | awk '$8 == "start_kernel" {printf "0x%s", $2}')
0072         [[ -z $start_kernel_addr ]] && return
0073 
0074         local file_line=$(addr2line -e $objfile $start_kernel_addr)
0075         [[ -z $file_line ]] && return
0076 
0077         local prefix=${file_line%init/main.c:*}
0078         if [[ -z $prefix ]] || [[ $prefix = $file_line ]]; then
0079                 return
0080         fi
0081 
0082         DIR_PREFIX=$prefix
0083         return 0
0084 }
0085 
0086 __faddr2line() {
0087         local objfile=$1
0088         local func_addr=$2
0089         local dir_prefix=$3
0090         local print_warnings=$4
0091 
0092         local func=${func_addr%+*}
0093         local offset=${func_addr#*+}
0094         offset=${offset%/*}
0095         local size=
0096         [[ $func_addr =~ "/" ]] && size=${func_addr#*/}
0097 
0098         if [[ -z $func ]] || [[ -z $offset ]] || [[ $func = $func_addr ]]; then
0099                 warn "bad func+offset $func_addr"
0100                 DONE=1
0101                 return
0102         fi
0103 
0104         # Go through each of the object's symbols which match the func name.
0105         # In rare cases there might be duplicates.
0106         while read symbol; do
0107                 local fields=($symbol)
0108                 local sym_base=0x${fields[0]}
0109                 local sym_type=${fields[1]}
0110                 local sym_end=0x${fields[3]}
0111 
0112                 # calculate the size
0113                 local sym_size=$(($sym_end - $sym_base))
0114                 if [[ -z $sym_size ]] || [[ $sym_size -le 0 ]]; then
0115                         warn "bad symbol size: base: $sym_base end: $sym_end"
0116                         DONE=1
0117                         return
0118                 fi
0119                 sym_size=0x$(printf %x $sym_size)
0120 
0121                 # calculate the address
0122                 local addr=$(($sym_base + $offset))
0123                 if [[ -z $addr ]] || [[ $addr = 0 ]]; then
0124                         warn "bad address: $sym_base + $offset"
0125                         DONE=1
0126                         return
0127                 fi
0128                 addr=0x$(printf %x $addr)
0129 
0130                 # weed out non-function symbols
0131                 if [[ $sym_type != t ]] && [[ $sym_type != T ]]; then
0132                         [[ $print_warnings = 1 ]] &&
0133                                 echo "skipping $func address at $addr due to non-function symbol of type '$sym_type'"
0134                         continue
0135                 fi
0136 
0137                 # if the user provided a size, make sure it matches the symbol's size
0138                 if [[ -n $size ]] && [[ $size -ne $sym_size ]]; then
0139                         [[ $print_warnings = 1 ]] &&
0140                                 echo "skipping $func address at $addr due to size mismatch ($size != $sym_size)"
0141                         continue;
0142                 fi
0143 
0144                 # make sure the provided offset is within the symbol's range
0145                 if [[ $offset -gt $sym_size ]]; then
0146                         [[ $print_warnings = 1 ]] &&
0147                                 echo "skipping $func address at $addr due to size mismatch ($offset > $sym_size)"
0148                         continue
0149                 fi
0150 
0151                 # separate multiple entries with a blank line
0152                 [[ $FIRST = 0 ]] && echo
0153                 FIRST=0
0154 
0155                 # pass real address to addr2line
0156                 echo "$func+$offset/$sym_size:"
0157                 addr2line -fpie $objfile $addr | sed "s; $dir_prefix\(\./\)*; ;"
0158                 DONE=1
0159 
0160         done < <(nm -n $objfile | awk -v fn=$func '$3 == fn { found=1; line=$0; start=$1; next } found == 1 { found=0; print line, $1 }')
0161 }
0162 
0163 [[ $# -lt 2 ]] && usage
0164 
0165 objfile=$1
0166 [[ ! -f $objfile ]] && die "can't find objfile $objfile"
0167 shift
0168 
0169 DIR_PREFIX=supercalifragilisticexpialidocious
0170 find_dir_prefix $objfile
0171 
0172 FIRST=1
0173 while [[ $# -gt 0 ]]; do
0174         func_addr=$1
0175         shift
0176 
0177         # print any matches found
0178         DONE=0
0179         __faddr2line $objfile $func_addr $DIR_PREFIX 0
0180 
0181         # if no match was found, print warnings
0182         if [[ $DONE = 0 ]]; then
0183                 __faddr2line $objfile $func_addr $DIR_PREFIX 1
0184                 warn "no match for $func_addr"
0185         fi
0186 done