aboutsummaryrefslogtreecommitdiffstats
path: root/util/parse-backtrace.sh
blob: 3db96f8e1e0d87f49fa4faa04015be09e2bdb95a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/bin/bash

tmpfile=`mktemp backtrace.XXX`

PROGRAM_PATH=''
TRACE_TYPE=glibc

print_help()
{
    echo "Usage: "
    echo "   " `basename $0`" [-s] [-p <path/to/filename>] -t <\"full trace\">"
    echo "Options: "
    echo "    -t   '<trace>'  - The entire trace should be put into quotes"
    echo "                      for this option"
    echo "    -p   path/to/filename  - optional path to the binary, typically"
    echo "                            autodetected"
    echo
    exit 1
}

if [ -z "$1" -o "$1" = "-h" -o "$1" = "--help" ]; then
    rm -f $tmpfile
    print_help
fi

while getopts "hf:t:p:" opt; do
    case $opt in
    h)
        print_help
        ;;
    f)
        PROGRAM_PATH="$OPTARG"
        ;;
    t)
        TRACE="$OPTARG"
        ;;
    p)
        PROGRAM_PATH="$OPTARG"
        ;;
    *)
        print_help
        ;;
        esac
done


# use addr2line
parse_glibc_trace()
{
    local trace="$1"
    local filename="$2"
    local tmpname=""
    local symbol=""

    IFS=$'\n'
    for line in ${trace}; do

        #echo "Line: '$line"

        # remove C2 A0 (non breaking space, as inserted by windows)
        line=$(echo $line | sed 's/\xC2\xA0/ /g')

        line=`echo $line | egrep "\[" | egrep "\]"`
        [ -n "$line" ] || continue

        # cut off additional syslog part - beginning of line to ':'
        line=$(echo $line line | sed -e 's/.*://')

        # parse lines like
        # /usr/lib/libfuse3.so.3(+0x1c0ef) [0x7fca6061c0ef]

        filename=$(echo $line | awk '{print $1}' | sed -e 's/(.*$//')
        if [ -z "${filename}" ]; then
            echo "Failed to get filename path for line: \"$line\""
            return
        fi

        if [[ $filename != /* ]]; then
            if [ -n "${PROGRAM_PATH}" ]; then
                filename="${PROGRAM_PATH}"
            else
                tmpname="$(which $filename)"
                if [ $? -ne 0 ]; then
                    echo "Failed to get path for '$filename'"
                    continue
                fi
                filename="${tmpname}"
            fi
        fi

        # for plain glibc backtrace_symbols the symbol is also in column1,
        # within the brackets ()
        symbol=$(echo $line | awk '{print $1}' | sed -e 's/^.*(//' | sed -e 's/).*//')
        if [ -z "${symbol}" ]; then
            echo "Failed to get symbol for line: \"$line\""
            continue
        fi

        addr2line -a -p -s -C -f -i -e ${filename} ${symbol}
    done
}

if [ -z "$TRACE" ]; then
    echo "Missing backtrace option!"
    echo
    print_help
fi


# For now only glibc backtrace_symbols traces are supported
if [ $TRACE_TYPE = "glibc" ]; then
    parse_glibc_trace "$TRACE" "${PROGRAM_PATH}"
else
    echo "Unknown tracetype: '${TRACE_TYPE}'"
fi

rm -f $tmpfile