1# Copyright (C) 2013, 2015 Red Hat, Inc.
2# This file is part of elfutils.
3#
4# This file is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; either version 3 of the License, or
7# (at your option) any later version.
8#
9# elfutils is distributed in the hope that it will be useful, but
10# WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17. $srcdir/test-subr.sh
18
19# Verify one of the backtraced threads contains function 'main'.
20check_main()
21{
22  if grep -w main $1; then
23    return
24  fi
25  echo >&2 $2: no main
26  false
27}
28
29# Without proper ELF symbols resolution we could get inappropriate weak
30# symbol "gsignal" with the same address as the correct symbol "raise".
31# It was fixed by GIT commit 78dec228b3cfb2f9300cd0b682ebf416c9674c91 .
32# [patch] Improve ELF symbols preference (global > weak)
33# https://lists.fedorahosted.org/pipermail/elfutils-devel/2012-October/002624.html
34check_gsignal()
35{
36  if ! grep -w gsignal $1; then
37    return
38  fi
39  echo >&2 $2: found gsignal
40  false
41}
42
43# Verify the STDERR output does not contain unexpected errors.
44# In some cases we cannot reliably find out we got behind _start as some
45# operating system do not properly terminate CFI by undefined PC.
46# Ignore it here as it is a bug of OS, not a bug of elfutils.
47check_err()
48{
49  if [ $(egrep -v <$1 'dwfl_thread_getframes: (No DWARF information found|no matching address range)$' \
50         | wc -c) \
51       -eq 0 ]
52  then
53    return
54  fi
55  echo >&2 $2: neither empty nor just out of DWARF
56  false
57}
58
59check_all()
60{
61  bt=$1
62  err=$2
63  testname=$3
64  check_main $bt $testname
65  check_gsignal $bt $testname
66  check_err $err $testname
67}
68
69check_unsupported()
70{
71  err=$1
72  testname=$2
73  if grep -q ': Unwinding not supported for this architecture$' $err; then
74    echo >&2 $testname: arch not supported
75    exit 77
76  fi
77}
78
79check_native_unsupported()
80{
81  err=$1
82  testname=$2
83  check_unsupported $err $testname
84
85  # ARM is special. It is supported, but it doesn't use .eh_frame by default
86  # making the native tests fail unless debuginfo (for glibc) is installed
87  # and we can fall back on .debug_frame for the CFI.
88  case "`uname -m`" in
89    arm* )
90      if egrep 'dwfl_thread_getframes(.*)No DWARF information found' $err; then
91	echo >&2 $testname: arm needs debuginfo installed for all libraries
92	exit 77
93      fi
94    ;;
95  esac
96}
97
98check_core()
99{
100  arch=$1
101  testfiles backtrace.$arch.{exec,core}
102  tempfiles backtrace.$arch.{bt,err}
103  echo ./backtrace ./backtrace.$arch.{exec,core}
104  testrun ${abs_builddir}/backtrace -e ./backtrace.$arch.exec --core=./backtrace.$arch.core 1>backtrace.$arch.bt 2>backtrace.$arch.err || true
105  cat backtrace.$arch.{bt,err}
106  check_unsupported backtrace.$arch.err backtrace.$arch.core
107  check_all backtrace.$arch.{bt,err} backtrace.$arch.core
108}
109
110# Backtrace live process.
111# Do not abort on non-zero exit code due to some warnings of ./backtrace
112# - see function check_err.
113check_native()
114{
115  child=$1
116  tempfiles $child.{bt,err}
117  (set +ex; testrun ${abs_builddir}/backtrace --backtrace-exec=${abs_builddir}/$child 1>$child.bt 2>$child.err; true)
118  cat $child.{bt,err}
119  check_native_unsupported $child.err $child
120  check_all $child.{bt,err} $child
121}
122
123# Backtrace core file.
124check_native_core()
125{
126  child=$1
127
128  # Disable valgrind while dumping core.
129  SAVED_VALGRIND_CMD="$VALGRIND_CMD"
130  unset VALGRIND_CMD
131
132  # Skip the test if we cannot adjust core ulimit.
133  core="core.`ulimit -c unlimited || exit 77; set +ex; testrun ${abs_builddir}/$child --gencore; true`"
134  # see if /proc/sys/kernel/core_uses_pid is set to 0
135  if [ -f core ]; then
136    mv core "$core"
137  fi
138  if [ ! -f "$core" ]; then echo "No $core file generated"; exit 77; fi
139
140  if [ "x$SAVED_VALGRIND_CMD" != "x" ]; then
141    VALGRIND_CMD="$SAVED_VALGRIND_CMD"
142    export VALGRIND_CMD
143  fi
144
145  # Do not abort on non-zero exit code due to some warnings of ./backtrace
146  # - see function check_err.
147  tempfiles $core{,.{bt,err}}
148  (set +ex; testrun ${abs_builddir}/backtrace -e ${abs_builddir}/$child --core=$core 1>$core.bt 2>$core.err; true)
149  cat $core.{bt,err}
150  check_native_unsupported $core.err $child-$core
151  check_all $core.{bt,err} $child-$core
152}
153