Brian Silverman | 8649792 | 2018-02-10 19:28:39 -0500 | [diff] [blame] | 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'. |
| 20 | check_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 |
| 34 | check_gsignal() |
| 35 | { |
| 36 | if ! grep -w gsignal $1; then |
| 37 | return |
| 38 | fi |
| 39 | echo >&2 $2: found gsignal |
| 40 | false |
| 41 | } |
| 42 | |
| 43 | |
| 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. |
| 47 | check_backtracegen() |
| 48 | { |
| 49 | if grep -w backtracegen $1; then |
| 50 | return |
| 51 | fi |
| 52 | echo >&2 $2: no backtracegen |
| 53 | false |
| 54 | } |
| 55 | |
| 56 | # 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. |
| 60 | check_err() |
| 61 | { |
| 62 | if [ $(egrep -v <$1 'dwfl_thread_getframes: (No DWARF information found|no matching address range|address out of range|Invalid register|\(null\))$' \ |
| 63 | | 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 | |
| 72 | check_all() |
| 73 | { |
| 74 | bt=$1 |
| 75 | err=$2 |
| 76 | testname=$3 |
| 77 | check_main $bt $testname |
| 78 | check_gsignal $bt $testname |
| 79 | check_err $err $testname |
| 80 | } |
| 81 | |
| 82 | check_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 |
| 90 | } |
| 91 | |
| 92 | check_native_unsupported() |
| 93 | { |
| 94 | err=$1 |
| 95 | testname=$2 |
| 96 | check_unsupported $err $testname |
| 97 | |
| 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* ) |
| 103 | if egrep 'dwfl_thread_getframes(.*)No DWARF information found' $err; then |
| 104 | echo >&2 $testname: arm needs debuginfo installed for all libraries |
| 105 | exit 77 |
| 106 | fi |
| 107 | ;; |
| 108 | esac |
| 109 | } |
| 110 | |
| 111 | check_core() |
| 112 | { |
| 113 | arch=$1 |
| 114 | testfiles backtrace.$arch.{exec,core} |
| 115 | tempfiles backtrace.$arch.{bt,err} |
| 116 | echo ./backtrace ./backtrace.$arch.{exec,core} |
| 117 | testrun ${abs_builddir}/backtrace -e ./backtrace.$arch.exec --core=./backtrace.$arch.core 1>backtrace.$arch.bt 2>backtrace.$arch.err || true |
| 118 | cat backtrace.$arch.{bt,err} |
| 119 | check_unsupported backtrace.$arch.err backtrace.$arch.core |
| 120 | check_all backtrace.$arch.{bt,err} backtrace.$arch.core |
| 121 | check_backtracegen backtrace.$arch.bt backtrace.$arch.core |
| 122 | } |
| 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. |
| 127 | check_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} |
| 133 | check_native_unsupported $child.err $child |
| 134 | check_all $child.{bt,err} $child |
| 135 | } |
| 136 | |
| 137 | # Backtrace core file. |
| 138 | check_native_core() |
| 139 | { |
| 140 | # systemd-coredump/coredumpctl doesn't seem to like concurrent core dumps |
| 141 | # use a lock file (fd 200) tests/core-dump-backtrace.lock |
| 142 | ( |
| 143 | child=$1 |
| 144 | |
| 145 | # Disable valgrind while dumping core. |
| 146 | SAVED_VALGRIND_CMD="$VALGRIND_CMD" |
| 147 | unset VALGRIND_CMD |
| 148 | |
| 149 | # Wait for lock for 10 seconds or skip. |
| 150 | flock -x -w 10 200 || exit 77; |
| 151 | |
| 152 | # Skip the test if we cannot adjust core ulimit. |
| 153 | pid="`ulimit -c unlimited || exit 77; set +ex; testrun ${abs_builddir}/$child --gencore; true`" |
| 154 | core="core.$pid" |
| 155 | # see if /proc/sys/kernel/core_uses_pid is set to 0 |
| 156 | if [ -f core ]; then |
| 157 | mv core "$core" |
| 158 | fi |
| 159 | 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 |
| 177 | echo "No $core file generated"; |
| 178 | exit 77; |
| 179 | fi |
| 180 | |
| 181 | if [ "x$SAVED_VALGRIND_CMD" != "x" ]; then |
| 182 | VALGRIND_CMD="$SAVED_VALGRIND_CMD" |
| 183 | export VALGRIND_CMD |
| 184 | fi |
| 185 | |
| 186 | # Do not abort on non-zero exit code due to some warnings of ./backtrace |
| 187 | # - see function check_err. |
| 188 | tempfiles $core{,.{bt,err}} |
| 189 | (set +ex; testrun ${abs_builddir}/backtrace -e ${abs_builddir}/$child --core=$core 1>$core.bt 2>$core.err; true) |
| 190 | cat $core.{bt,err} |
| 191 | check_native_unsupported $core.err $child-$core |
| 192 | check_all $core.{bt,err} $child-$core |
| 193 | rm $core{,.{bt,err}} |
| 194 | ) 200>${abs_builddir}/core-dump-backtrace.lock |
| 195 | } |