• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
18
19while test -n "${1}"; do
20    case "${1}" in
21    -o | --objdir)
22        objdir="${2}"
23        shift
24        shift
25        ;;
26    -p | --symbolizer)
27        symbolizer="${2:-addr2line}"
28        shift
29        shift
30        ;;
31    -h | --help)
32        shift
33        help=yes
34        break
35        ;;
36    *)
37        shift
38        break
39        ;;
40    esac
41done
42
43print_help() {
44    cat <<-END
45Usage: symbolize.sh [OPTION]...
46Translate call stack to symbolized forms.
47
48    Options:
49
50    -o,  --objdir  <objects_dir>    dir that contains the call stack binaries.
51    -p,  --symbolizer <symbolizer>  symbolizer for translating call stacks, default is addr2line.
52    -h,  --help                     print help info
53END
54}
55
56test $help = yes && print_help && exit 0
57
58find_unstripped() {
59    file="$(basename $1)"
60    shift
61    if [ "${file:0:8}" = "ld-musl-" ]; then
62        file="libc.so"
63    fi
64    find "$@" -type f -name "$file" -exec sh -c  'file -L $1 | grep -q "not stripped"' sh {} \; -print
65}
66
67find_file() {
68    find_unstripped $1 $objdir
69}
70
71getsym2() {
72    files="$(find_file $1)"
73    for file in $files; do
74        if [ -f "$file" ]; then
75            $symbolizer -C -f -p -e "${file}" $2 2>/dev/null
76        fi
77    done
78}
79
80getsym() {
81    if [ $# -gt 0 ]; then
82        getsym2 ${1//+/ }
83    fi
84}
85
86while read -r line; do
87    echo "$line" $(getsym $(echo "$line" | sed -n 's/.*(\(.*\)).*/\1/p'))
88done
89