blob: ff42c6ffda26b414015d7fa0f4cfa07be64d30a5 [file] [log] [blame]
Mark Wielaard7ec8d272015-05-30 23:00:40 +02001# Copyright (C) 2013, 2015 Red Hat, Inc.
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +01002# 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
Mark Wielaard5e661502017-04-25 12:41:58 +020043
44# Makes sure we saw the function that initiated the backtrace
45# when the core was generated through the tests backtrace --gencore.
46# This might disappear when frame pointer chasing gone bad.
47check_backtracegen()
48{
49 if grep -w backtracegen $1; then
50 return
51 fi
52 echo >&2 $2: no backtracegen
53 false
54}
55
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +010056# Verify the STDERR output does not contain unexpected errors.
57# In some cases we cannot reliably find out we got behind _start as some
58# operating system do not properly terminate CFI by undefined PC.
59# Ignore it here as it is a bug of OS, not a bug of elfutils.
60check_err()
61{
Mark Wielaard660aafc2017-05-21 23:33:15 +020062 if [ $(egrep -v <$1 'dwfl_thread_getframes: (No DWARF information found|no matching address range|address out of range|Invalid register|\(null\))$' \
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +010063 | wc -c) \
64 -eq 0 ]
65 then
66 return
67 fi
68 echo >&2 $2: neither empty nor just out of DWARF
69 false
70}
71
72check_all()
73{
74 bt=$1
75 err=$2
76 testname=$3
Mark Wielaard727a90c2017-04-24 13:35:47 +020077 check_main $bt $testname
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +010078 check_gsignal $bt $testname
79 check_err $err $testname
80}
81
82check_unsupported()
83{
84 err=$1
85 testname=$2
86 if grep -q ': Unwinding not supported for this architecture$' $err; then
87 echo >&2 $testname: arch not supported
88 exit 77
89 fi
Mark Wielaardcb5ac6e2014-06-14 01:01:58 +020090}
91
92check_native_unsupported()
93{
94 err=$1
95 testname=$2
96 check_unsupported $err $testname
Mark Wielaard225dddf2014-01-26 20:16:48 +010097
98 # ARM is special. It is supported, but it doesn't use .eh_frame by default
99 # making the native tests fail unless debuginfo (for glibc) is installed
100 # and we can fall back on .debug_frame for the CFI.
101 case "`uname -m`" in
102 arm* )
Mark Wielaardcdfc8532014-12-19 18:33:52 +0100103 if egrep 'dwfl_thread_getframes(.*)No DWARF information found' $err; then
Mark Wielaard225dddf2014-01-26 20:16:48 +0100104 echo >&2 $testname: arm needs debuginfo installed for all libraries
105 exit 77
106 fi
107 ;;
108 esac
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100109}
110
111check_core()
112{
113 arch=$1
114 testfiles backtrace.$arch.{exec,core}
115 tempfiles backtrace.$arch.{bt,err}
116 echo ./backtrace ./backtrace.$arch.{exec,core}
Mark Wielaard727a90c2017-04-24 13:35:47 +0200117 testrun ${abs_builddir}/backtrace -e ./backtrace.$arch.exec --core=./backtrace.$arch.core 1>backtrace.$arch.bt 2>backtrace.$arch.err || true
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100118 cat backtrace.$arch.{bt,err}
Kurt Roeckx02cefda2014-04-22 21:46:22 +0200119 check_unsupported backtrace.$arch.err backtrace.$arch.core
Mark Wielaard727a90c2017-04-24 13:35:47 +0200120 check_all backtrace.$arch.{bt,err} backtrace.$arch.core
Mark Wielaard5e661502017-04-25 12:41:58 +0200121 check_backtracegen backtrace.$arch.bt backtrace.$arch.core
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100122}
123
124# Backtrace live process.
125# Do not abort on non-zero exit code due to some warnings of ./backtrace
126# - see function check_err.
127check_native()
128{
129 child=$1
130 tempfiles $child.{bt,err}
131 (set +ex; testrun ${abs_builddir}/backtrace --backtrace-exec=${abs_builddir}/$child 1>$child.bt 2>$child.err; true)
132 cat $child.{bt,err}
Mark Wielaardcb5ac6e2014-06-14 01:01:58 +0200133 check_native_unsupported $child.err $child
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100134 check_all $child.{bt,err} $child
135}
136
137# Backtrace core file.
138check_native_core()
139{
Mark Wielaard699a7412017-12-23 23:16:24 +0100140# systemd-coredump/coredumpctl doesn't seem to like concurrent core dumps
141# use a lock file (fd 200) tests/core-dump-backtrace.lock
142(
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100143 child=$1
Mark Wielaard7fc49292013-12-13 22:42:46 +0100144
145 # Disable valgrind while dumping core.
146 SAVED_VALGRIND_CMD="$VALGRIND_CMD"
147 unset VALGRIND_CMD
148
Mark Wielaard699a7412017-12-23 23:16:24 +0100149 # Wait for lock for 10 seconds or skip.
150 flock -x -w 10 200 || exit 77;
151
Mark Wielaard1051a0c2014-01-04 15:41:04 +0100152 # Skip the test if we cannot adjust core ulimit.
Mark Wielaard699a7412017-12-23 23:16:24 +0100153 pid="`ulimit -c unlimited || exit 77; set +ex; testrun ${abs_builddir}/$child --gencore; true`"
154 core="core.$pid"
Matthias Klosee922ec42014-01-07 10:25:29 +0100155 # see if /proc/sys/kernel/core_uses_pid is set to 0
156 if [ -f core ]; then
157 mv core "$core"
158 fi
Mark Wielaard699a7412017-12-23 23:16:24 +0100159 type -P coredumpctl && have_coredumpctl=1 || have_coredumpctl=0
160 if [ ! -f "$core" -a $have_coredumpctl -eq 1 ]; then
161 # Maybe systemd-coredump took it. But give it some time to dump first...
162 sleep 1
163 coredumpctl --output="$core" dump $pid || rm -f $core
164
165 # Try a couple of times after waiting some more if something went wrong...
166 if [ ! -f "$core" ]; then
167 sleep 2
168 coredumpctl --output="$core" dump $pid || rm -f $core
169 fi
170
171 if [ ! -f "$core" ]; then
172 sleep 3
173 coredumpctl --output="$core" dump $pid || rm -f $core
174 fi
175 fi
176 if [ ! -f "$core" ]; then
Mark Wielaard61e33d72018-02-16 20:34:25 +0100177 # In some containers our view of pids is confused. Since tests are
178 # run in a new fresh directory any core here is most like is ours.
179 if ls core.[0-9]* 1> /dev/null 2>&1; then
180 mv core.[0-9]* "$core"
181 fi
182 fi
183 if [ ! -f "$core" ]; then
Mark Wielaard699a7412017-12-23 23:16:24 +0100184 echo "No $core file generated";
185 exit 77;
186 fi
Mark Wielaard7fc49292013-12-13 22:42:46 +0100187
188 if [ "x$SAVED_VALGRIND_CMD" != "x" ]; then
189 VALGRIND_CMD="$SAVED_VALGRIND_CMD"
190 export VALGRIND_CMD
191 fi
192
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100193 # Do not abort on non-zero exit code due to some warnings of ./backtrace
194 # - see function check_err.
195 tempfiles $core{,.{bt,err}}
196 (set +ex; testrun ${abs_builddir}/backtrace -e ${abs_builddir}/$child --core=$core 1>$core.bt 2>$core.err; true)
197 cat $core.{bt,err}
Mark Wielaardcb5ac6e2014-06-14 01:01:58 +0200198 check_native_unsupported $core.err $child-$core
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100199 check_all $core.{bt,err} $child-$core
Mark Wielaard699a7412017-12-23 23:16:24 +0100200 rm $core{,.{bt,err}}
201) 200>${abs_builddir}/core-dump-backtrace.lock
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100202}