1#!/bin/bash 2# Copyright (c) 2022 Huawei Device Co., Ltd. 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15objdir=${SYSROOT:-.} 16symbolizer=${SYMBOLIZER:-addr2line} 17help=no 18max_count=10000 19 20while test -n "${1}"; do 21 case "${1}" in 22 -o | --objdir) 23 objdir="${2}" 24 shift 25 ;; 26 -p | --symbolizer) 27 symbolizer="${2}" 28 shift 29 ;; 30 --max-count) 31 max_count="${2}" 32 shift 33 ;; 34 -h | --help) 35 help=yes 36 break 37 ;; 38 *) 39 echo "$0: Warning: unsupported parameter $1" >&2 40 ;; 41 esac 42 shift 43done 44 45print_help() { 46 cat <<-END 47Usage: symbolize.sh [OPTION]... 48Translate call stack to symbolized forms. 49 50 Options: 51 52 -o, --objdir <objects_dir> dir that contains the call stack binaries. 53 -p, --symbolizer <symbolizer> symbolizer for translating call stacks, default is addr2line. 54 --max-count <max-count> max calls of symbolizer for translatings, default is 10000. 55 -h, --help print help info 56END 57} 58 59test $help = yes && print_help && exit 0 60 61find_unstripped() { 62 file="$(basename $1)" 63 shift 64 if [ "${file:0:8}" = "ld-musl-" ]; then 65 file="libc.so" 66 fi 67 find "$@" -type f -name "$file" -exec sh -c 'file -L $1 | grep -q "not stripped"' sh {} \; -print 68} 69 70find_file() { 71 find_unstripped $1 $objdir 72} 73 74getsym2() { 75 cur_count=$((${cur_count:-0}+1)) 76 if [ $cur_count -gt $max_count ]; then 77 sym="(resolve count $cur_count exceeds max_count $max_count)" 78 return 79 fi 80 files="$(find_file $1)" 81 for file in $files; do 82 if [ -f "$file" ]; then 83 sym="$sym $($symbolizer -C -f -p -e "${file}" $2 2>/dev/null)" 84 fi 85 done 86} 87 88getsym() { 89 if [ $# -gt 0 ]; then 90 getsym2 ${1//+/ } 91 fi 92} 93 94while read -r line; do 95 unset sym 96 getsym $(echo "$line" | sed -n 's/.*(\(.*+.*\)).*/\1/p') 97 echo "$line" "$sym" 98done 99