103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes# Copyright (C) 2013 Red Hat, Inc.
203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes# This file is part of elfutils.
303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#
403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes# This file is free software; you can redistribute it and/or modify
503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes# it under the terms of the GNU General Public License as published by
603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes# the Free Software Foundation; either version 3 of the License, or
703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes# (at your option) any later version.
803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#
903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes# elfutils is distributed in the hope that it will be useful, but
1003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes# WITHOUT ANY WARRANTY; without even the implied warranty of
1103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes# GNU General Public License for more details.
1303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#
1403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes# You should have received a copy of the GNU General Public License
1503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
1703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes. $srcdir/test-subr.sh
1803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
1903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes# Verify one of the backtraced threads contains function 'main'.
2003333823c75a1c1887e923828113a1b0fd12020cElliott Hughescheck_main()
2103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes{
2203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  if grep -w main $1; then
2303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes    return
2403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  fi
2503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  echo >&2 $2: no main
2603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  false
2703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes}
2803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
2903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes# Without proper ELF symbols resolution we could get inappropriate weak
3003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes# symbol "gsignal" with the same address as the correct symbol "raise".
3103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes# It was fixed by GIT commit 78dec228b3cfb2f9300cd0b682ebf416c9674c91 .
3203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes# [patch] Improve ELF symbols preference (global > weak)
3303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes# https://lists.fedorahosted.org/pipermail/elfutils-devel/2012-October/002624.html
3403333823c75a1c1887e923828113a1b0fd12020cElliott Hughescheck_gsignal()
3503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes{
3603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  if ! grep -w gsignal $1; then
3703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes    return
3803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  fi
3903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  echo >&2 $2: found gsignal
4003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  false
4103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes}
4203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
4303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes# Verify the STDERR output does not contain unexpected errors.
4403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes# In some cases we cannot reliably find out we got behind _start as some
4503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes# operating system do not properly terminate CFI by undefined PC.
4603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes# Ignore it here as it is a bug of OS, not a bug of elfutils.
4703333823c75a1c1887e923828113a1b0fd12020cElliott Hughescheck_err()
4803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes{
4903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  if [ $(egrep -v <$1 'dwfl_thread_getframes: (No DWARF information found|no matching address range)$' \
5003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes         | wc -c) \
5103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes       -eq 0 ]
5203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  then
5303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes    return
5403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  fi
5503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  echo >&2 $2: neither empty nor just out of DWARF
5603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  false
5703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes}
5803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
5903333823c75a1c1887e923828113a1b0fd12020cElliott Hughescheck_all()
6003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes{
6103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  bt=$1
6203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  err=$2
6303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  testname=$3
6403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  check_main $bt $testname
6503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  check_gsignal $bt $testname
6603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  check_err $err $testname
6703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes}
6803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
6903333823c75a1c1887e923828113a1b0fd12020cElliott Hughescheck_unsupported()
7003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes{
7103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  err=$1
7203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  testname=$2
7303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  if grep -q ': Unwinding not supported for this architecture$' $err; then
7403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes    echo >&2 $testname: arch not supported
7503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes    exit 77
7603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  fi
7703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes}
7803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
7903333823c75a1c1887e923828113a1b0fd12020cElliott Hughescheck_native_unsupported()
8003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes{
8103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  err=$1
8203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  testname=$2
8303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  check_unsupported $err $testname
8403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
8503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  # ARM is special. It is supported, but it doesn't use .eh_frame by default
8603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  # making the native tests fail unless debuginfo (for glibc) is installed
8703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  # and we can fall back on .debug_frame for the CFI.
8803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  case "`uname -m`" in
8903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes    arm* )
9003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes      if egrep 'dwfl_thread_getframes(.*)No DWARF information found' $err; then
9103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	echo >&2 $testname: arm needs debuginfo installed for all libraries
9203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	exit 77
9303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes      fi
9403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes    ;;
9503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  esac
9603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes}
9703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
9803333823c75a1c1887e923828113a1b0fd12020cElliott Hughescheck_core()
9903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes{
10003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  arch=$1
10103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  testfiles backtrace.$arch.{exec,core}
10203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  tempfiles backtrace.$arch.{bt,err}
10303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  echo ./backtrace ./backtrace.$arch.{exec,core}
10403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  testrun ${abs_builddir}/backtrace -e ./backtrace.$arch.exec --core=./backtrace.$arch.core 1>backtrace.$arch.bt 2>backtrace.$arch.err || true
10503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  cat backtrace.$arch.{bt,err}
10603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  check_unsupported backtrace.$arch.err backtrace.$arch.core
10703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  check_all backtrace.$arch.{bt,err} backtrace.$arch.core
10803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes}
10903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
11003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes# Backtrace live process.
11103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes# Do not abort on non-zero exit code due to some warnings of ./backtrace
11203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes# - see function check_err.
11303333823c75a1c1887e923828113a1b0fd12020cElliott Hughescheck_native()
11403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes{
11503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  child=$1
11603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  tempfiles $child.{bt,err}
11703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  (set +ex; testrun ${abs_builddir}/backtrace --backtrace-exec=${abs_builddir}/$child 1>$child.bt 2>$child.err; true)
11803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  cat $child.{bt,err}
11903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  check_native_unsupported $child.err $child
12003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  check_all $child.{bt,err} $child
12103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes}
12203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
12303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes# Backtrace core file.
12403333823c75a1c1887e923828113a1b0fd12020cElliott Hughescheck_native_core()
12503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes{
12603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  child=$1
12703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
12803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  # Disable valgrind while dumping core.
12903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  SAVED_VALGRIND_CMD="$VALGRIND_CMD"
13003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  unset VALGRIND_CMD
13103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
13203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  # Skip the test if we cannot adjust core ulimit.
13303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  core="core.`ulimit -c unlimited || exit 77; set +ex; testrun ${abs_builddir}/$child --gencore; true`"
13403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  # see if /proc/sys/kernel/core_uses_pid is set to 0
13503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  if [ -f core ]; then
13603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes    mv core "$core"
13703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  fi
13803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  if [ ! -f "$core" ]; then exit 77; fi
13903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
14003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  if [ "x$SAVED_VALGRIND_CMD" != "x" ]; then
14103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes    VALGRIND_CMD="$SAVED_VALGRIND_CMD"
14203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes    export VALGRIND_CMD
14303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  fi
14403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
14503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  # Do not abort on non-zero exit code due to some warnings of ./backtrace
14603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  # - see function check_err.
14703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  tempfiles $core{,.{bt,err}}
14803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  (set +ex; testrun ${abs_builddir}/backtrace -e ${abs_builddir}/$child --core=$core 1>$core.bt 2>$core.err; true)
14903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  cat $core.{bt,err}
15003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  check_native_unsupported $core.err $child-$core
15103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  check_all $core.{bt,err} $child-$core
15203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes}
153