This is the mail archive of the elfutils-devel@sourceware.org mailing list for the elfutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH 2/3] Add frame pointer unwinding as fallback on arm


From: Ulf Hermann <ulf.hermann@qt.io>

If we don't find any debug information for a given frame, we usually
cannot unwind any further. However, the binary in question might have
been compiled with frame pointers, in which case we can look up the
well known frame pointer locations in the stack snapshot and use them
to bridge the frames without debug information.

At the moment this works only for ARM code. THUMB code uses a
different mechanism for unwinding. Also, in order to figure out if
a function was compiled in ARM or in THUMB mode we need a symbol
table which might not be available (e.g. with JIT-compiled code).

Signed-off-by: Ulf Hermann <ulf.hermann@qt.io>
---
 backends/ChangeLog                 |   6 +++
 backends/Makefile.am               |   3 +-
 backends/arm_init.c                |   1 +
 backends/arm_unwind.c              |  88 +++++++++++++++++++++++++++++++++++++
 tests/ChangeLog                    |   7 +++
 tests/Makefile.am                  |   3 ++
 tests/backtrace.arm.fp.core.bz2    | Bin 0 -> 10997 bytes
 tests/backtrace.arm.fp.exec.bz2    | Bin 0 -> 245414 bytes
 tests/run-backtrace-fp-core-arm.sh |  37 ++++++++++++++++
 9 files changed, 144 insertions(+), 1 deletion(-)
 create mode 100644 backends/arm_unwind.c
 create mode 100644 tests/backtrace.arm.fp.core.bz2
 create mode 100755 tests/backtrace.arm.fp.exec.bz2
 create mode 100755 tests/run-backtrace-fp-core-arm.sh

diff --git a/backends/ChangeLog b/backends/ChangeLog
index 371f071..6837a49 100644
--- a/backends/ChangeLog
+++ b/backends/ChangeLog
@@ -1,5 +1,11 @@
 2017-02-09  Ulf Hermann  <ulf.hermann@qt.io>
 
+	* arm_unwind.c: New file
+	* Makefile.am (arm_SRCS): Add arm_unwind.c
+	* arm_init.c (arm_init): Hook arm_unwind
+
+2017-02-09  Ulf Hermann  <ulf.hermann@qt.io>
+
 	* x86_64_unwind.c: New file
 	* Makefile.am (x86_64_SRCS): Add x86_64_unwind.c
 	* x86_64_init.c (x86_64_init): Hook x86_64_unwind
diff --git a/backends/Makefile.am b/backends/Makefile.am
index 60917b9..14cbf04 100644
--- a/backends/Makefile.am
+++ b/backends/Makefile.am
@@ -74,7 +74,8 @@ libebl_alpha_pic_a_SOURCES = $(alpha_SRCS)
 am_libebl_alpha_pic_a_OBJECTS = $(alpha_SRCS:.c=.os)
 
 arm_SRCS = arm_init.c arm_symbol.c arm_regs.c arm_corenote.c \
-	   arm_auxv.c arm_attrs.c arm_retval.c arm_cfi.c arm_initreg.c
+	   arm_auxv.c arm_attrs.c arm_retval.c arm_cfi.c arm_initreg.c \
+	   arm_unwind.c
 libebl_arm_pic_a_SOURCES = $(arm_SRCS)
 am_libebl_arm_pic_a_OBJECTS = $(arm_SRCS:.c=.os)
 
diff --git a/backends/arm_init.c b/backends/arm_init.c
index caadac6..4fa0601 100644
--- a/backends/arm_init.c
+++ b/backends/arm_init.c
@@ -68,6 +68,7 @@ arm_init (Elf *elf __attribute__ ((unused)),
   /* We only unwind the core integer registers.  */
   eh->frame_nregs = 16;
   HOOK (eh, set_initial_registers_tid);
+  HOOK (eh, unwind);
 
   /* Bit zero encodes whether an function address is THUMB or ARM. */
   eh->func_addr_mask = ~(GElf_Addr)1;
diff --git a/backends/arm_unwind.c b/backends/arm_unwind.c
new file mode 100644
index 0000000..c20ebc2
--- /dev/null
+++ b/backends/arm_unwind.c
@@ -0,0 +1,88 @@
+/* Get previous frame state for an existing frame state.
+   Copyright (C) 2016 The Qt Company Ltd.
+   This file is part of elfutils.
+
+   This file is free software; you can redistribute it and/or modify
+   it under the terms of either
+
+     * the GNU Lesser General Public License as published by the Free
+       Software Foundation; either version 3 of the License, or (at
+       your option) any later version
+
+   or
+
+     * the GNU General Public License as published by the Free
+       Software Foundation; either version 2 of the License, or (at
+       your option) any later version
+
+   or both in parallel, as here.
+
+   elfutils is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received copies of the GNU General Public License and
+   the GNU Lesser General Public License along with this program.  If
+   not, see <http://www.gnu.org/licenses/>.  */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <assert.h>
+
+#ifndef BACKEND
+#define BACKEND arm_
+#define FP_REG 11
+#define LR_REG 14
+#define SP_REG 13
+#define FP_OFFSET 4
+#define LR_OFFSET 0
+#define SP_OFFSET 8
+#endif
+
+#include "libebl_CPU.h"
+
+/* There was no CFI. Maybe we happen to have a frame pointer and can unwind from that?  */
+
+bool
+EBLHOOK(unwind) (Ebl *ebl __attribute__ ((unused)), Dwarf_Addr pc __attribute__ ((unused)),
+                 ebl_tid_registers_t *setfunc, ebl_tid_registers_get_t *getfunc,
+                 ebl_pid_memory_read_t *readfunc, void *arg,
+                 bool *signal_framep __attribute__ ((unused)))
+{
+  Dwarf_Word fp, lr, sp;
+
+  if (!getfunc(LR_REG, 1, &lr, arg))
+    return false;
+
+  if (!setfunc(-1, 1, &lr, arg))
+    return false;
+
+  if (!getfunc(FP_REG, 1, &fp, arg))
+    fp = 0;
+
+  if (!getfunc(SP_REG, 1, &sp, arg))
+    sp = 0;
+
+  Dwarf_Word newLr, newFp, newSp;
+
+  if (!readfunc(fp + LR_OFFSET, &newLr, arg))
+    newLr = 0;
+
+  if (!readfunc(fp + FP_OFFSET, &newFp, arg))
+    newFp = 0;
+
+  newSp = fp + SP_OFFSET;
+
+  // These are not fatal if they don't work. They will just prevent unwinding at the next frame.
+  setfunc(LR_REG, 1, &newLr, arg);
+  setfunc(FP_REG, 1, &newFp, arg);
+  setfunc(SP_REG, 1, &newSp, arg);
+
+  // If the fp is invalid, we might still have a valid lr.
+  // But if the fp is valid, then the stack should be moving in the right direction.
+  return lr != 0 && (fp == 0 || newSp > sp);
+}
diff --git a/tests/ChangeLog b/tests/ChangeLog
index e24f51b..2c210ea 100644
--- a/tests/ChangeLog
+++ b/tests/ChangeLog
@@ -1,5 +1,12 @@
 2017-02-09  Ulf Hermann  <ulf.hermann@qt.io>
 
+	* Makefile.am: Add test for unwinding with frame pointers on arm
+	* backtrace.arm.fp.core.bz2: New file
+	* backtrace.arm.fp.exec.bz2: New file
+	* run-backtrace-fp-core-arm.sh: New file
+
+2017-02-09  Ulf Hermann  <ulf.hermann@qt.io>
+
 	* Makefile.am: Add test for unwinding with frame pointers on x86_64
 	* backtrace.x86_64.fp.core.bz2: New file
 	* backtrace.x86_64.fp.exec.bz2: New file
diff --git a/tests/Makefile.am b/tests/Makefile.am
index dd9ae81..b72befc 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -114,6 +114,7 @@ TESTS = run-arextract.sh run-arsymtest.sh newfile test-nlist \
 	run-backtrace-native-biarch.sh run-backtrace-native-core.sh \
 	run-backtrace-native-core-biarch.sh run-backtrace-core-x86_64.sh \
 	run-backtrace-fp-core-x86_64.sh \
+	run-backtrace-fp-core-arm.sh \
 	run-backtrace-core-x32.sh \
 	run-backtrace-core-i386.sh run-backtrace-core-ppc.sh \
 	run-backtrace-core-s390x.sh run-backtrace-core-s390.sh \
@@ -293,6 +294,8 @@ EXTRA_DIST = run-arextract.sh run-arsymtest.sh \
 	     run-backtrace-core-x86_64.sh run-backtrace-core-i386.sh \
 	     run-backtrace-fp-core-x86_64.sh \
 	     run-backtrace-core-x32.sh \
+	     run-backtrace-fp-core-arm.sh \
+	     backtrace.arm.fp.core.bz2 backtrace.arm.fp.exec.bz2 \
 	     backtrace-subr.sh backtrace.i386.core.bz2 backtrace.i386.exec.bz2 \
 	     backtrace.x86_64.core.bz2 backtrace.x86_64.exec.bz2 \
 	     backtrace.x86_64.fp.core.bz2 backtrace.x86_64.fp.exec.bz2 \
diff --git a/tests/run-backtrace-fp-core-arm.sh b/tests/run-backtrace-fp-core-arm.sh
new file mode 100755
index 0000000..33e2ddf
--- /dev/null
+++ b/tests/run-backtrace-fp-core-arm.sh
@@ -0,0 +1,37 @@
+#! /bin/bash
+# Copyright (C) 2017 The Qt Company
+# This file is part of elfutils.
+#
+# This file is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# elfutils is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+. $srcdir/backtrace-subr.sh
+
+# The binary is generated by compiling backtrace-child without debug
+# information, but with -mapcs-frame -fno-omit-frame-pointer.
+# -mapcs-frame makes some functions store the frame pointer in r11.
+# gcc docs say one should also specify -fno-omit-frame-pointer, but
+# result seems to be independent of this.
+#
+# gcc -static -O2 -mapcs-frame -fno-omit-frame-pointer \
+#     -mfloat-abi=hard -I ../ -I ../lib/ -D_GNU_SOURCE \
+#     -pthread -o backtrace.arm.fp.exec backtrace-child.c \
+#     --sysroot=/my/arm/sysroot
+#
+# Then strip the .eh_frame section from the binary:
+#
+# strip -R .eh_frame backtrace.arm.fp.exec
+#
+# The core is generated by calling the binary with --gencore
+
+check_core arm.fp --allow-unknown
-- 
1.8.3.1


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]