| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 |
|
| 6 | usage() {
|
| 7 | echo "Usage:"
|
| 8 | echo " $0 [-R] -r <release>"
|
| 9 | echo " $0 [-R] [<vmlinux> [<base_path>|auto [<modules_path>]]]"
|
| 10 | echo " $0 -h"
|
| 11 | echo "Options:"
|
| 12 | echo " -R: decode return address instead of caller address."
|
| 13 | }
|
| 14 |
|
| 15 | |
| 16 | if type llvm-cxxfilt >/dev/null 2>&1 ; then
|
| 17 | cppfilt=llvm-cxxfilt
|
| 18 | elif type c++filt >/dev/null 2>&1 ; then
|
| 19 | cppfilt=c++filt
|
| 20 | cppfilt_opts=-i
|
| 21 | fi
|
| 22 |
|
| 23 | UTIL_SUFFIX=
|
| 24 | if [[ -z ${LLVM:-} ]]; then
|
| 25 | UTIL_PREFIX=${CROSS_COMPILE:-}
|
| 26 | else
|
| 27 | UTIL_PREFIX=llvm-
|
| 28 | if [[ ${LLVM} == */ ]]; then
|
| 29 | UTIL_PREFIX=${LLVM}${UTIL_PREFIX}
|
| 30 | elif [[ ${LLVM} == -* ]]; then
|
| 31 | UTIL_SUFFIX=${LLVM}
|
| 32 | fi
|
| 33 | fi
|
| 34 |
|
| 35 | READELF=${UTIL_PREFIX}readelf${UTIL_SUFFIX}
|
| 36 | ADDR2LINE=${UTIL_PREFIX}addr2line${UTIL_SUFFIX}
|
| 37 | NM=${UTIL_PREFIX}nm${UTIL_SUFFIX}
|
| 38 | decode_retaddr=false
|
| 39 |
|
| 40 | if [[ $1 == "-h" ]] ; then
|
| 41 | usage
|
| 42 | exit 0
|
| 43 | elif [[ $1 == "-R" ]] ; then
|
| 44 | decode_retaddr=true
|
| 45 | shift 1
|
| 46 | fi
|
| 47 |
|
| 48 | if [[ $1 == "-r" ]] ; then
|
| 49 | vmlinux=""
|
| 50 | basepath="auto"
|
| 51 | modpath=""
|
| 52 | release=$2
|
| 53 |
|
| 54 | for fn in {,/usr/lib/debug}/boot/vmlinux-$release{,.debug} /lib/modules/$release{,/build}/vmlinux ; do
|
| 55 | if [ -e "$fn" ] ; then
|
| 56 | vmlinux=$fn
|
| 57 | break
|
| 58 | fi
|
| 59 | done
|
| 60 |
|
| 61 | if [[ $vmlinux == "" ]] ; then
|
| 62 | echo "ERROR! vmlinux image for release $release is not found" >&2
|
| 63 | usage
|
| 64 | exit 2
|
| 65 | fi
|
| 66 | else
|
| 67 | vmlinux=$1
|
| 68 | basepath=${2-auto}
|
| 69 | modpath=$3
|
| 70 | release=""
|
| 71 | debuginfod=
|
| 72 |
|
| 73 | |
| 74 | if type debuginfod-find >/dev/null 2>&1 ; then
|
| 75 | debuginfod=${1-only}
|
| 76 | fi
|
| 77 |
|
| 78 | if [[ $vmlinux == "" && -z $debuginfod ]] ; then
|
| 79 | echo "ERROR! vmlinux image must be specified" >&2
|
| 80 | usage
|
| 81 | exit 1
|
| 82 | fi
|
| 83 | fi
|
| 84 |
|
| 85 | declare aarray_support=true
|
| 86 | declare -A cache 2>/dev/null
|
| 87 | if [[ $? != 0 ]]; then
|
| 88 | aarray_support=false
|
| 89 | else
|
| 90 | declare -A modcache
|
| 91 | fi
|
| 92 |
|
| 93 | find_module() {
|
| 94 | if [[ -n $debuginfod ]] ; then
|
| 95 | if [[ -n $modbuildid ]] ; then
|
| 96 | debuginfod-find debuginfo $modbuildid && return
|
| 97 | fi
|
| 98 |
|
| 99 | |
| 100 | if [[ $debuginfod == "only" ]] ; then
|
| 101 | return
|
| 102 | fi
|
| 103 | fi
|
| 104 |
|
| 105 | if [ -z $release ] ; then
|
| 106 | release=$(gdb -ex 'print init_uts_ns.name.release' -ex 'quit' -quiet -batch "$vmlinux" 2>/dev/null | sed -n 's/\$1 = "\(.*\)".*/\1/p')
|
| 107 | fi
|
| 108 | if [ -n "${release}" ] ; then
|
| 109 | release_dirs="/usr/lib/debug/lib/modules/$release /lib/modules/$release"
|
| 110 | fi
|
| 111 |
|
| 112 | found_without_debug_info=false
|
| 113 | for dir in "$modpath" "$(dirname "$vmlinux")" ${release_dirs}; do
|
| 114 | if [ -n "${dir}" ] && [ -e "${dir}" ]; then
|
| 115 | for fn in $(find "$dir" -name "${module//_/[-_]}.ko*") ; do
|
| 116 | if ${READELF} -WS "$fn" | grep -qwF .debug_line ; then
|
| 117 | echo $fn
|
| 118 | return
|
| 119 | fi
|
| 120 | found_without_debug_info=true
|
| 121 | done
|
| 122 | fi
|
| 123 | done
|
| 124 |
|
| 125 | if [[ ${found_without_debug_info} == true ]]; then
|
| 126 | echo "WARNING! No debugging info in module ${module}, rebuild with DEBUG_KERNEL and DEBUG_INFO" >&2
|
| 127 | else
|
| 128 | echo "WARNING! Cannot find .ko for module ${module}, please pass a valid module path" >&2
|
| 129 | fi
|
| 130 |
|
| 131 | return 1
|
| 132 | }
|
| 133 |
|
| 134 | parse_symbol() {
|
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 |
|
| 141 | if [[ $module == "" ]] ; then
|
| 142 | local objfile=$vmlinux
|
| 143 | elif [[ $aarray_support == true && "${modcache[$module]+isset}" == "isset" ]]; then
|
| 144 | local objfile=${modcache[$module]}
|
| 145 | else
|
| 146 | local objfile=$(find_module)
|
| 147 | if [[ $objfile == "" ]] ; then
|
| 148 | return
|
| 149 | fi
|
| 150 | if [[ $aarray_support == true ]]; then
|
| 151 | modcache[$module]=$objfile
|
| 152 | fi
|
| 153 | fi
|
| 154 |
|
| 155 | |
| 156 | symbol=${symbol#\(}
|
| 157 | symbol=${symbol%\)}
|
| 158 |
|
| 159 | |
| 160 | local segment
|
| 161 | if [[ $symbol == *:* ]] ; then
|
| 162 | segment=${symbol%%:*}:
|
| 163 | symbol=${symbol#*:}
|
| 164 | fi
|
| 165 |
|
| 166 | |
| 167 | local name=${symbol%+*}
|
| 168 |
|
| 169 | |
| 170 | |
| 171 | |
| 172 | if [[ $aarray_support == true && "${cache[$module,$name]+isset}" == "isset" ]]; then
|
| 173 | local base_addr=${cache[$module,$name]}
|
| 174 | else
|
| 175 | local base_addr=$(${NM} "$objfile" 2>/dev/null | awk '$3 == "'$name'" && ($2 == "t" || $2 == "T") {print $1; exit}')
|
| 176 | if [[ $base_addr == "" ]] ; then
|
| 177 | |
| 178 | return
|
| 179 | fi
|
| 180 | if [[ $aarray_support == true ]]; then
|
| 181 | cache[$module,$name]="$base_addr"
|
| 182 | fi
|
| 183 | fi
|
| 184 | |
| 185 | |
| 186 | local expr=${symbol%/*}
|
| 187 | |
| 188 | local offset=${expr#*+}
|
| 189 | offset=$((offset))
|
| 190 |
|
| 191 | |
| 192 | |
| 193 | expr=${expr/$name/0x$base_addr}
|
| 194 |
|
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | if [[ $decode_retaddr == false && $offset != 0 ]]; then
|
| 200 | expr=$((expr-1))
|
| 201 | else
|
| 202 | expr=$((expr))
|
| 203 | fi
|
| 204 | local address=$(printf "%x\n" "$expr")
|
| 205 |
|
| 206 | |
| 207 | |
| 208 | if [[ $aarray_support == true && "${cache[$module,$address]+isset}" == "isset" ]]; then
|
| 209 | local code=${cache[$module,$address]}
|
| 210 | else
|
| 211 | local code=$(${ADDR2LINE} -i -e "$objfile" "$address" 2>/dev/null)
|
| 212 | if [[ $aarray_support == true ]]; then
|
| 213 | cache[$module,$address]=$code
|
| 214 | fi
|
| 215 | fi
|
| 216 |
|
| 217 | |
| 218 | |
| 219 | |
| 220 | if [[ $code == "??:0" ]]; then
|
| 221 | return
|
| 222 | fi
|
| 223 |
|
| 224 | |
| 225 | code=$(while read -r line; do echo "${line#$basepath/}"; done <<< "$code")
|
| 226 |
|
| 227 | |
| 228 | code=${code//$'\n'/' '}
|
| 229 |
|
| 230 | |
| 231 | |
| 232 | if [[ $name =~ ^_R && $cppfilt != "" ]] ; then
|
| 233 | name=$("$cppfilt" "$cppfilt_opts" "$name")
|
| 234 | fi
|
| 235 |
|
| 236 | |
| 237 | symbol="$segment$name ($code)"
|
| 238 | }
|
| 239 |
|
| 240 | debuginfod_get_vmlinux() {
|
| 241 | local vmlinux_buildid=${1##* }
|
| 242 |
|
| 243 | if [[ $vmlinux != "" ]]; then
|
| 244 | return
|
| 245 | fi
|
| 246 |
|
| 247 | if [[ $vmlinux_buildid =~ ^[0-9a-f]+ ]]; then
|
| 248 | vmlinux=$(debuginfod-find debuginfo $vmlinux_buildid)
|
| 249 | if [[ $? -ne 0 ]] ; then
|
| 250 | echo "ERROR! vmlinux image not found via debuginfod-find" >&2
|
| 251 | usage
|
| 252 | exit 2
|
| 253 | fi
|
| 254 | return
|
| 255 | fi
|
| 256 | echo "ERROR! Build ID for vmlinux not found. Try passing -r or specifying vmlinux" >&2
|
| 257 | usage
|
| 258 | exit 2
|
| 259 | }
|
| 260 |
|
| 261 | decode_code() {
|
| 262 | local scripts=`dirname "${BASH_SOURCE[0]}"`
|
| 263 | local lim="Code: "
|
| 264 |
|
| 265 | echo -n "${1%%${lim}*}"
|
| 266 | echo "${lim}${1##*${lim}}" | $scripts/decodecode
|
| 267 | }
|
| 268 |
|
| 269 | handle_line() {
|
| 270 | if [[ $basepath == "auto" && $vmlinux != "" ]] ; then
|
| 271 | module=""
|
| 272 | symbol="kernel_init+0x0/0x0"
|
| 273 | parse_symbol
|
| 274 | basepath=${symbol#kernel_init (}
|
| 275 | basepath=${basepath%/init/main.c:*)}
|
| 276 | fi
|
| 277 |
|
| 278 | local words spaces
|
| 279 |
|
| 280 | |
| 281 | read -ra words <<<"$1"
|
| 282 | IFS='#' read -ra spaces <<<"$(shopt -s extglob; echo "${1//+([^[:space:]])/#}")"
|
| 283 |
|
| 284 | |
| 285 | |
| 286 |
|
| 287 | |
| 288 | |
| 289 | local last=$(( ${#words[@]} - 1 ))
|
| 290 |
|
| 291 | for i in "${!words[@]}"; do
|
| 292 | |
| 293 | if [[ ${words[$i]} =~ \[\<([^]]+)\>\] ]]; then
|
| 294 | unset words[$i] spaces[$i]
|
| 295 | fi
|
| 296 | done
|
| 297 |
|
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | local info_str=""
|
| 304 | if [[ ${words[$last]} =~ \([A-Z]*\) ]]; then
|
| 305 | info_str=${words[$last]}
|
| 306 | unset words[$last] spaces[$last]
|
| 307 | last=$(( $last - 1 ))
|
| 308 | fi
|
| 309 |
|
| 310 | |
| 311 | |
| 312 | if [[ ${words[$last]} =~ ^[0-9a-f]+\] ]]; then
|
| 313 | words[$last-1]="${words[$last-1]} ${words[$last]}"
|
| 314 | unset words[$last] spaces[$last]
|
| 315 | last=$(( $last - 1 ))
|
| 316 | fi
|
| 317 |
|
| 318 | if [[ ${words[$last]} =~ \[([^]]+)\] ]]; then
|
| 319 | module=${words[$last]}
|
| 320 | |
| 321 | |
| 322 | module=${module%\)}
|
| 323 | module=${module#\[}
|
| 324 | module=${module%\]}
|
| 325 | modbuildid=${module#* }
|
| 326 | module=${module% *}
|
| 327 | if [[ $modbuildid == $module ]]; then
|
| 328 | modbuildid=
|
| 329 | fi
|
| 330 | symbol=${words[$last-1]}
|
| 331 | unset words[$last-1] spaces[$last-1]
|
| 332 | else
|
| 333 | |
| 334 | symbol=${words[$last]}
|
| 335 | module=
|
| 336 | modbuildid=
|
| 337 | fi
|
| 338 |
|
| 339 | unset words[$last]
|
| 340 | parse_symbol |
| 341 |
|
| 342 | |
| 343 | for i in "${!words[@]}"; do
|
| 344 | echo -n "${spaces[i]}${words[i]}"
|
| 345 | done
|
| 346 | echo "${spaces[$last]}${symbol}${module:+ ${module}}${info_str:+ ${info_str}}"
|
| 347 | }
|
| 348 |
|
| 349 | while read line; do
|
| 350 | |
| 351 | line=${line%$'\r'}
|
| 352 |
|
| 353 | |
| 354 | if [[ $line =~ \[\<([^]]+)\>\] ]] ||
|
| 355 | [[ $line =~ [^+\ ]+\+0x[0-9a-f]+/0x[0-9a-f]+ ]]; then
|
| 356 | |
| 357 | handle_line "$line"
|
| 358 | |
| 359 | elif [[ $line == *Code:* ]]; then
|
| 360 | decode_code "$line"
|
| 361 | |
| 362 | elif [[ -n $debuginfod && $line =~ PID:\ [0-9]+\ Comm: ]]; then
|
| 363 | debuginfod_get_vmlinux "$line"
|
| 364 | else
|
| 365 | |
| 366 | echo "$line"
|
| 367 | fi
|
| 368 | done
|