• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2
3#Copyright (c) 2020-2021 Huawei Device Co., Ltd.
4#Licensed under the Apache License, Version 2.0 (the "License");
5#you may not use this file except in compliance with the License.
6#You may obtain a copy of the License at
7#
8#    http://www.apache.org/licenses/LICENSE-2.0
9#
10#Unless required by applicable law or agreed to in writing, software
11#distributed under the License is distributed on an "AS IS" BASIS,
12#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13#See the License for the specific language governing permissions and
14#limitations under the License.
15
16LOAD_BASE="0x2000000"
17LLVM_ADDR2LINE=llvm-addr2line
18GCC_ADDR2LINE=addr2line
19
20get_line()
21{
22    SYM_ADDR=$(echo $1 | awk '{print $2}')
23    ELF_OFFSET=`echo ${SYM_ADDR} | cut -d '[' -f2 | cut -d ']' -f1`
24    FILE_LINE=`${ADDR2LINE} -f -e $2 ${ELF_OFFSET} | awk 'NR==2'`
25    if [[ "${FILE_LINE}" == *"?"* ]]; then
26        typeset ELF_OFFSET=$((ELF_OFFSET+LOAD_BASE))
27        ELF_OFFSET=$(echo "obase=16;${ELF_OFFSET}" | bc)
28        FILE_LINE=`${ADDR2LINE} -f -e $2 ${ELF_OFFSET} | awk 'NR==2'`
29    fi
30    echo ${FILE_LINE}
31}
32
33parse_line()
34{
35    FILE_PATH=$(echo $1 | awk '{print $4}')
36    ELF_FILE=${FILE_PATH##*/}
37    ELF_FILE=${ELF_FILE//[$'\t\r\n']}
38    FLAG=false
39    for i in $2; do
40        if [ "${ELF_FILE}" = "$i" ]; then
41            if [ ! -f "$i" ]; then
42                echo "Error: no such file: $i"
43                exit 1
44            fi
45            FILE_LINE=`get_line "$1" $i`
46            if [[ "${FILE_LINE}" == *"?"* ]] || [ -z "${FILE_LINE}" ]; then
47                echo "        * Error: you need ensure whether file: "$i" was compiled with -g or not! *"
48                exit 1
49            fi
50            LINE=`echo $1 | tr -d '\r'`
51            LINE=$(echo ${LINE} | awk '{print $1,$2}')
52            echo "        "${LINE}" at "${FILE_LINE}
53            FLAG=true
54            break
55        fi
56    done
57    if [[ "${FLAG}" == "false" ]]; then
58        echo "        "$1
59    fi
60}
61
62if [ $# -le 1 ]; then
63    echo "Usage: ./out2line.sh text.txt elf1 elf2 ..."
64    exit 1
65fi
66
67read -n5 -p "Compiler is [gcc/llvm]: " ANSWER
68case ${ANSWER} in
69(gcc | GCC)
70    which ${GCC_ADDR2LINE} >/dev/null 2>&1
71    if [ $? -eq 0 ]; then
72        ADDR2LINE=${GCC_ADDR2LINE}
73    else
74        echo "${GCC_ADDR2LINE} command not found!"
75        exit 1
76    fi;;
77(llvm | LLVM)
78    which ${LLVM_ADDR2LINE} >/dev/null 2>&1
79    if [ $? -eq 0 ]; then
80        ADDR2LINE=${LLVM_ADDR2LINE}
81    else
82        echo "${LLVM_ADDR2LINE} command not found! Trying to use ${GCC_ADDR2LINE}!"
83        which ${GCC_ADDR2LINE} >/dev/null 2>&1
84        if [ $? -eq 0 ]; then
85            ADDR2LINE=${GCC_ADDR2LINE}
86        else
87            echo "${GCC_ADDR2LINE} command not found!"
88            exit 1
89        fi
90    fi;;
91(*)
92    echo "Error choise!"
93    exit 1
94esac
95echo -e "Now using ${ADDR2LINE} ...\n"
96
97PARAM_LIST="${*:2}"
98cat $1 | while read line; do
99    HEAD_STRING=$(echo ${line} | awk '{print $1}')
100    if [[ "${HEAD_STRING}" == *"#"* ]]; then
101        parse_line "${line}" "${PARAM_LIST}"
102    else
103        if [[ "${HEAD_STRING}" == *"["* ]]; then
104            echo "    "${line}
105        else
106            echo ${line}
107        fi
108    fi
109done
110
111