Skip to contentSkip to conversation

ARM: UNWINDER_FRAME_POINTER implementation for Clang

PATCH Superseded Nathan Huckleberry 3 participants 9 messages Aug 20, 2019 – Aug 23, 2019 Arm (32-bit)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ARM: UNWINDER_FRAME_POINTER implementation for Clang

The stackframe setup when compiled with clang is different. Since the stack unwinder expects the gcc stackframe setup it fails to print backtraces. This patch adds support for the clang stackframe setup.

---
Changes from RFC
* Push extra register to satisfy 8 byte alignment requirement
* Add clarifying comments
* Separate code into its own file

 arch/arm/Kconfig.debug         |   4 +-
 arch/arm/Makefile              |   5 +-
 arch/arm/lib/Makefile          |   8 +-
 arch/arm/lib/backtrace-clang.S | 224 +++++++++++++++++++++++++++++++++
 4 files changed, 237 insertions(+), 4 deletions(-)
 create mode 100644 arch/arm/lib/backtrace-clang.S
arch/arm/Kconfig.debug +2 -2
index 85710e078afb..92fca7463e21 100644
@@ -56,7 +56,7 @@ choice
 
 config UNWINDER_FRAME_POINTER
 	bool "Frame pointer unwinder"
-	depends on !THUMB2_KERNEL && !CC_IS_CLANG
+	depends on !THUMB2_KERNEL
 	select ARCH_WANT_FRAME_POINTERS
 	select FRAME_POINTER
 	help
@@ -1872,7 +1872,7 @@ config DEBUG_UNCOMPRESS
 	  When this option is set, the selected DEBUG_LL output method
 	  will be re-used for normal decompressor output on multiplatform
 	  kernels.
-	  
+
 
 config UNCOMPRESS_INCLUDE
 	string
arch/arm/Makefile +4 -1
index c3624ca6c0bc..729e223b83fe 100644
@@ -36,7 +36,10 @@ KBUILD_CFLAGS	+= $(call cc-option,-mno-unaligned-access)
 endif
 
 ifeq ($(CONFIG_FRAME_POINTER),y)
-KBUILD_CFLAGS	+=-fno-omit-frame-pointer -mapcs -mno-sched-prolog
+KBUILD_CFLAGS	+=-fno-omit-frame-pointer
+  ifeq ($(CONFIG_CC_IS_GCC),y)
+  KBUILD_CFLAGS += -mapcs -mno-sched-prolog
+  endif
 endif
 
 ifeq ($(CONFIG_CPU_BIG_ENDIAN),y)
arch/arm/lib/Makefile +7 -1
index b25c54585048..e10a769c72ec 100644
@@ -5,7 +5,7 @@
 # Copyright (C) 1995-2000 Russell King
 #
 
-lib-y		:= backtrace.o changebit.o csumipv6.o csumpartial.o   \
+lib-y		:= changebit.o csumipv6.o csumpartial.o               \
 		   csumpartialcopy.o csumpartialcopyuser.o clearbit.o \
 		   delay.o delay-loop.o findbit.o memchr.o memcpy.o   \
 		   memmove.o memset.o setbit.o                        \
@@ -19,6 +19,12 @@ lib-y		:= backtrace.o changebit.o csumipv6.o csumpartial.o   \
 mmu-y		:= clear_user.o copy_page.o getuser.o putuser.o       \
 		   copy_from_user.o copy_to_user.o
 
+ifdef CONFIG_CC_IS_CLANG
+  lib-y += backtrace-clang.o
+else
+  lib-y += backtrace.o
+endif
+
 # using lib_ here won't override already available weak symbols
 obj-$(CONFIG_UACCESS_WITH_MEMCPY) += uaccess_with_memcpy.o
 
arch/arm/lib/backtrace-clang.S new file +224 -0

224 lines not on this page · raw message

Signature · 1 line
2.23.0.rc1.153.gdeed80330f-goog
#1 lore raw
Quoted 10 lines · Nathan Huckleberry

The stackframe setup when compiled with clang is different. Since the stack unwinder expects the gcc stackframe setup it fails to print backtraces. This patch adds support for the clang stackframe setup.

Quoted 5 lines

---
Changes from RFC
* Push extra register to satisfy 8 byte alignment requirement
* Add clarifying comments
* Separate code into its own file

Thanks for the patch! The added comments and moving the implementation to its own file make it easier to review.

Quoted 22 lines
 arch/arm/Kconfig.debug         |   4 +-
 arch/arm/Makefile              |   5 +-
 arch/arm/lib/Makefile          |   8 +-
 arch/arm/lib/backtrace-clang.S | 224 +++++++++++++++++++++++++++++++++
 4 files changed, 237 insertions(+), 4 deletions(-)
 create mode 100644 arch/arm/lib/backtrace-clang.S
arch/arm/Kconfig.debug +1 -1
index 85710e078afb..92fca7463e21 100644
@@ -56,7 +56,7 @@ choice
 
 config UNWINDER_FRAME_POINTER
        bool "Frame pointer unwinder"
-       depends on !THUMB2_KERNEL && !CC_IS_CLANG
+       depends on !THUMB2_KERNEL
        select ARCH_WANT_FRAME_POINTERS
        select FRAME_POINTER
        help

arch/arm/Kconfig.debug config DEBUG_UNCOMPRESS · line 1875

Quoted 5 lines
arch/arm/Kconfig.debug +1 -1
@@ -1872,7 +1872,7 @@ config DEBUG_UNCOMPRESS
          When this option is set, the selected DEBUG_LL output method
          will be re-used for normal decompressor output on multiplatform
          kernels.
-
+

Probably can drop the added newline?

Quoted 8 lines
arch/arm/Kconfig.debug +0 -0
 config UNCOMPRESS_INCLUDE
        string
arch/arm/Makefile +0 -0
index c3624ca6c0bc..729e223b83fe 100644

arch/arm/Makefile KBUILD_CFLAGS += $(call cc-option,-mno-unaligned-access) · line 42

Quoted 8 lines
arch/arm/Makefile +4 -1
@@ -36,7 +36,10 @@ KBUILD_CFLAGS        += $(call cc-option,-mno-unaligned-access)
 endif
 
 ifeq ($(CONFIG_FRAME_POINTER),y)
-KBUILD_CFLAGS  +=-fno-omit-frame-pointer -mapcs -mno-sched-prolog
+KBUILD_CFLAGS  +=-fno-omit-frame-pointer
+  ifeq ($(CONFIG_CC_IS_GCC),y)
+  KBUILD_CFLAGS += -mapcs -mno-sched-prolog
+  endif

While I can appreciate the indentation, it's unusual to indent additional depths of kernel Makefiles. At least the rest of this file does not do so. Of course, the other Makefile you touch below does two spaces. At least try to keep the file internally consistent, even if the kernel itself is inconsistent.

Quoted 17 lines
arch/arm/Makefile +0 -0
 endif
 
 ifeq ($(CONFIG_CPU_BIG_ENDIAN),y)
arch/arm/lib/Makefile +1 -1
index b25c54585048..e10a769c72ec 100644
@@ -5,7 +5,7 @@
 # Copyright (C) 1995-2000 Russell King
 #
 
-lib-y          := backtrace.o changebit.o csumipv6.o csumpartial.o   \
+lib-y          := changebit.o csumipv6.o csumpartial.o               \
                   csumpartialcopy.o csumpartialcopyuser.o clearbit.o \
                   delay.o delay-loop.o findbit.o memchr.o memcpy.o   \
                   memmove.o memset.o setbit.o                        \

arch/arm/lib/Makefile lib-y := backtrace.o changebit.o csumipv6.o csumpartial.o \ · line 26

Quoted 8 lines
arch/arm/lib/Makefile +5 -0
@@ -19,6 +19,12 @@ lib-y                := backtrace.o changebit.o csumipv6.o csumpartial.o   \
 mmu-y          := clear_user.o copy_page.o getuser.o putuser.o       \
                   copy_from_user.o copy_to_user.o
 
+ifdef CONFIG_CC_IS_CLANG
+  lib-y += backtrace-clang.o
+else
+  lib-y += backtrace.o
+endif

The indentation should match the above (from this file). Looks like 1 tab after lib-y. See L34(CONFIG_CPU_32v3) for what I would have expected.

Quoted 13 lines
arch/arm/lib/Makefile +1 -0
+
 # using lib_ here won't override already available weak symbols
 obj-$(CONFIG_UACCESS_WITH_MEMCPY) += uaccess_with_memcpy.o
 
arch/arm/lib/backtrace-clang.S new file +3 -0
new file mode 100644
index 000000000000..2b02014dbdd1
@@ -0,0 +1,224 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ *  linux/arch/arm/lib/backtrace-clang.S

arch/arm/lib/backtrace-clang.S line 13

Quoted 10 lines
arch/arm/lib/backtrace-clang.S new file +10 -0
+ *
+ *  Copyright (C) 2019 Nathan Huckleberry
+ *
+ */
+#include <linux/kern_levels.h>
+#include <linux/linkage.h>
+#include <asm/assembler.h>
+               .text
+
+/* fp is 0 or stack frame */

ah, I see that the reference implementation uses an assembly comment here. Ok (sorry for the noise).

Quoted 11 lines
arch/arm/lib/backtrace-clang.S new file +11 -0
+
+#define frame  r4
+#define sv_fp  r5
+#define sv_pc  r6
+#define mask   r7
+#define sv_lr   r8
+
+ENTRY(c_backtrace)
+
+#if !defined(CONFIG_FRAME_POINTER) || !defined(CONFIG_PRINTK)
+               ret     lr

arch/arm/lib/backtrace-clang.S line 34

Quoted 10 lines
arch/arm/lib/backtrace-clang.S new file +10 -0
+ENDPROC(c_backtrace)
+#else
+
+
+/*
+ * Clang does not store pc or sp in function prologues
+ *             so we don't know exactly where the function
+ *             starts.
+ * We can treat the current frame's lr as the saved pc and the
+ *             preceding frame's lr as the lr, but we can't

preceding frame's lr as the current frame's lr, ...

arch/arm/lib/backtrace-clang.S line 46

Quoted 12 lines
arch/arm/lib/backtrace-clang.S new file +12 -0
+ *             trace the most recent call.
+ * Inserting a false stack frame allows us to reference the
+ *             function called last in the stacktrace.
+ * If the call instruction was a bl we can look at the callers
+ *             branch instruction to calculate the saved pc.
+ * We can recover the pc in most cases, but in cases such as
+ *             calling function pointers we cannot. In this
+ *             case, default to using the lr. This will be
+ *             some address in the function, but will not
+ *             be the function start.
+ * Unfortunately due to the stack frame layout we can't dump
+ *              r0 - r3, but these are less frequently saved.

The use of tabs vs spaces in these comments is inconsistent. Not that I can see whitespace, but:
https://github.com/nickdesaulniers/dotfiles/blob/37359525f5a403b4ed2d3f9d1bbbee2da8ec8115/.vimrc#L35-L41
Also, I don't think you need to tab indent every line after the first. Where did that format come from?

Quoted 22 lines
arch/arm/lib/backtrace-clang.S new file +22 -0
+ *
+ * Stack frame layout:
+ *             <larger addresses>
+ *             saved lr
+ *    frame => saved fp
+ *             optionally saved caller registers (r4 - r10)
+ *             optionally saved arguments (r0 - r3)
+ *             <top of stack frame>
+ *             <smaller addresses>
+ *
+ * Functions start with the following code sequence:
+ * corrected pc =>  stmfd sp!, {..., fp, lr}
+ *                 add fp, sp, #x
+ *                 stmfd sp!, {r0 - r3} (optional)
+ *
+ *
+ *
+ *
+ *
+ *
+ * The diagram below shows an example stack setup
+ *     for dump_stack.

arch/arm/lib/backtrace-clang.S line 78

Quoted 10 lines
arch/arm/lib/backtrace-clang.S new file +10 -0
+ *
+ * The frame for c_backtrace has pointers to the
+ *     code of dump_stack. This is why the frame of
+ *     c_backtrace is used to for the pc calculation
+ *     of dump_stack. This is why we must move back
+ *     a frame to print dump_stack.
+ *
+ * The stored locals for dump_stack are in dump_stack's
+ *     frame. This means that to fully print dump_stack's frame
+ *     we need the both the frame for dump_stack (for locals) and the

we need both the ...
(There's an extra the in the sentence).

Quoted 22 lines
arch/arm/lib/backtrace-clang.S new file +22 -0
+ *     frame that was called by dump_stack (for pc).
+ *
+ * To print locals we must know where the function start is. If
+ *     we read the function prologue opcodes we can determine
+ *     which variables are stored in the stack frame.
+ *
+ * To find the function start of dump_stack we can look at the
+ *     stored LR of show_stack. It points at the instruction
+ *     directly after the bl dump_stack. We can then read the
+ *     offset from the bl opcode to determine where the branch takes us.
+ *     The address calculated must be the start of dump_stack.
+ *
+ * c_backtrace frame           dump_stack:
+ * {[LR]    }  ============|   ...
+ * {[FP]    }  =======|    |   bl c_backtrace
+ *                    |    |=> ...
+ * {[R4-R10]}         |
+ * {[R0-R3] }         |        show_stack:
+ * dump_stack frame   |        ...
+ * {[LR]    } =============|   bl dump_stack
+ * {[FP]    } <=======|    |=> ...
+ * {[R4-R10]}

arch/arm/lib/backtrace-clang.S line 110

Quoted 10 lines
arch/arm/lib/backtrace-clang.S new file +10 -0
+ * {[R0-R3] }
+ */
+
+stmfd   sp!, {r4 - r9, fp, lr} @ Save an extra register
+                               @ to ensure 8 byte alignment
+movs   frame, r0               @ if frame pointer is zero
+beq    no_frame                @ we have no stack frames
+
+tst    r1, #0x10               @ 26 or 32-bit mode?
+moveq  mask, #0xfc000003

Should we be using different masks for ARM vs THUMB as per the reference implementation?

Quoted 9 lines
arch/arm/lib/backtrace-clang.S new file +9 -0
+movne  mask, #0                @ mask for 32-bit
+
+/*
+ * Switches the current frame to be the frame for dump_stack.
+ */
+               add     frame, sp, #24          @ switch to false frame
+for_each_frame:        tst     frame, mask             @ Check for address exceptions
+               bne     no_frame
+

arch/arm/lib/backtrace-clang.S line 129

Quoted 10 lines
arch/arm/lib/backtrace-clang.S new file +10 -0
+/*
+ * sv_fp is the stack frame with the locals for the current considered
+ *     function.
+ * sv_pc is the saved lr frame the frame above. This is a pointer to a
+ *     code address within the current considered function, but
+ *     it is not the function start. This value gets updated to be
+ *     the function start later if it is possible.
+ */
+1001:          ldr     sv_pc, [frame, #4]      @ get saved 'pc'
+1002:          ldr     sv_fp, [frame, #0]      @ get saved fp

The reference implementation applies the mask to sv_pc and sv_fp. I assume we want to, too?

Quoted 11 lines
arch/arm/lib/backtrace-clang.S new file +11 -0
+
+               teq     sv_fp, #0               @ make sure next frame exists
+               beq     no_frame
+
+/*
+ * sv_lr is the lr from the function that called the current function. This
+ *     is a pointer to a code address in the current function's caller.
+ *     sv_lr-4 is the instruction used to call the current function.
+ * This sv_lr can be used to calculate the function start if the function
+ *     was called using a bl instruction. If the function start
+ *     can be recovered sv_pc is overwritten with the function start.

arch/arm/lib/backtrace-clang.S line 150

Quoted 10 lines
arch/arm/lib/backtrace-clang.S new file +10 -0
+ * If the current function was called using a function pointer we cannot
+ *     recover the function start and instead continue with sv_pc as
+ *     an arbitrary value within the current function. If this is the case
+ *     we cannot print registers for the current function, but the stacktrace
+ *     is still printed properly.
+ */
+1003:          ldr     sv_lr, [sv_fp, #4]      @ get saved lr from next frame
+
+               ldr     r0, [sv_lr, #-4]        @ get call instruction
+               ldr     r3, .Ldsi+8

I wonder what dsi stands for, it could use a better name. Maybe put that mask in a more descriptively named section and use that instead of .Ldsi+8?

arch/arm/lib/backtrace-clang.S line 156

Quoted 6 lines
arch/arm/lib/backtrace-clang.S new file +6 -0
+               and     r2, r3, r0              @ is this a bl call
+               teq     r2, r3
+               bne     finished_setup          @ give up if it's not
+               and     r0, #0xffffff           @ get call offset 24-bit int
+               lsl     r0, r0, #8              @ sign extend offset
+               asr     r0, r0, #8

It's too bad this should work for older ARM versions, v6 added dedicated instructions for this:
http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0489c/Cihfifdd.html

Quoted 25 lines
arch/arm/lib/backtrace-clang.S new file +25 -0
+               ldr     sv_pc, [sv_fp, #4]      @ get lr address
+               add     sv_pc, sv_pc, #-4       @ get call instruction address
+               add     sv_pc, sv_pc, #8        @ take care of prefetch
+               add     sv_pc, sv_pc, r0, lsl #2 @ find function start
+
+finished_setup:
+
+               bic     sv_pc, sv_pc, mask      @ mask PC/LR for the mode
+
+/*
+ * Print the function (sv_pc) and where it was called
+ *     from (sv_lr).
+ */
+1004:          mov     r0, sv_pc
+
+               mov     r1, sv_lr
+               mov     r2, frame
+               bic     r1, r1, mask            @ mask PC/LR for the mode
+               bl      dump_backtrace_entry
+
+/*
+ * Test if the function start is a stmfd instruction
+ *     to determine which registers were stored in the function
+ *     prologue.
+ * If we could not recover the sv_pc because we were called through

arch/arm/lib/backtrace-clang.S line 191

Quoted 10 lines
arch/arm/lib/backtrace-clang.S new file +10 -0
+ *     a function pointer the comparison will fail and no registers
+ *     will print.
+ */
+1005:          ldr     r1, [sv_pc, #0]         @ if stmfd sp!, {..., fp, lr}
+               ldr     r3, .Ldsi               @ instruction exists,
+               teq     r3, r1, lsr #11
+               ldr     r0, [frame]             @ locals are stored in
+                                               @ the preceding frame
+               subeq   r0, r0, #4
+               bleq    dump_backtrace_stm      @ dump saved registers

Do we need to do anything to test .Ldsi+4? Otherwise looks like we define it but don't use it?

Quoted 36 lines
arch/arm/lib/backtrace-clang.S new file +33 -1
+
+/*
+ * If we are out of frames or if the next frame
+ *     is invalid.
+ */
+               teq     sv_fp, #0               @ zero saved fp means
+               beq     no_frame                @ no further frames
+
+               cmp     sv_fp, frame            @ next frame must be
+               mov     frame, sv_fp            @ above the current frame
+               bhi     for_each_frame
+
+1006:          adr     r0, .Lbad
+               mov     r1, frame
+               bl      printk
+no_frame:      ldmfd   sp!, {r4 - r9, fp, pc}
+ENDPROC(c_backtrace)
+               .pushsection __ex_table,"a"
+               .align  3
+               .long   1001b, 1006b
+               .long   1002b, 1006b
+               .long   1003b, 1006b
+               .long   1004b, 1006b
+               .long   1005b, 1006b
+               .popsection
+
+.Lbad:         .asciz  "Backtrace aborted due to bad frame pointer <%p>\n"
+               .align
+.Ldsi:         .word   0xe92d4800 >> 11        @ stmfd sp!, {... fp, lr}
+               .word   0xe92d0000 >> 11        @ stmfd sp!, {}
+               .word   0x0b000000              @ bl if these bits are set
+
+#endif
--

2.23.0.rc1.153.gdeed80330f-goog

Signature · 2 lines
Thanks,
~Nick Desaulniers
#2 lore raw
Quoted 237 lines · Nick Desaulniers
Quoted 10 lines · Nathan Huckleberry

The stackframe setup when compiled with clang is different. Since the stack unwinder expects the gcc stackframe setup it fails to print backtraces. This patch adds support for the clang stackframe setup.

Quoted 5 lines

---
Changes from RFC
* Push extra register to satisfy 8 byte alignment requirement
* Add clarifying comments
* Separate code into its own file

Thanks for the patch! The added comments and moving the implementation to its own file make it easier to review.

Quoted 22 lines
 arch/arm/Kconfig.debug         |   4 +-
 arch/arm/Makefile              |   5 +-
 arch/arm/lib/Makefile          |   8 +-
 arch/arm/lib/backtrace-clang.S | 224 +++++++++++++++++++++++++++++++++
 4 files changed, 237 insertions(+), 4 deletions(-)
 create mode 100644 arch/arm/lib/backtrace-clang.S
arch/arm/Kconfig.debug +1 -1
index 85710e078afb..92fca7463e21 100644
@@ -56,7 +56,7 @@ choice
 
 config UNWINDER_FRAME_POINTER
        bool "Frame pointer unwinder"
-       depends on !THUMB2_KERNEL && !CC_IS_CLANG
+       depends on !THUMB2_KERNEL
        select ARCH_WANT_FRAME_POINTERS
        select FRAME_POINTER
        help

arch/arm/Kconfig.debug config DEBUG_UNCOMPRESS · line 1875

Quoted 5 lines
arch/arm/Kconfig.debug +1 -1
@@ -1872,7 +1872,7 @@ config DEBUG_UNCOMPRESS
          When this option is set, the selected DEBUG_LL output method
          will be re-used for normal decompressor output on multiplatform
          kernels.
-
+

Probably can drop the added newline?

Quoted 8 lines
arch/arm/Kconfig.debug +0 -0
 config UNCOMPRESS_INCLUDE
        string
arch/arm/Makefile +0 -0
index c3624ca6c0bc..729e223b83fe 100644

arch/arm/Makefile KBUILD_CFLAGS += $(call cc-option,-mno-unaligned-access) · line 42

Quoted 8 lines
arch/arm/Makefile +4 -1
@@ -36,7 +36,10 @@ KBUILD_CFLAGS        += $(call cc-option,-mno-unaligned-access)
 endif
 
 ifeq ($(CONFIG_FRAME_POINTER),y)
-KBUILD_CFLAGS  +=-fno-omit-frame-pointer -mapcs -mno-sched-prolog
+KBUILD_CFLAGS  +=-fno-omit-frame-pointer
+  ifeq ($(CONFIG_CC_IS_GCC),y)
+  KBUILD_CFLAGS += -mapcs -mno-sched-prolog
+  endif

While I can appreciate the indentation, it's unusual to indent additional depths of kernel Makefiles. At least the rest of this file does not do so. Of course, the other Makefile you touch below does two spaces. At least try to keep the file internally consistent, even if the kernel itself is inconsistent.

Quoted 17 lines
arch/arm/Makefile +0 -0
 endif
 
 ifeq ($(CONFIG_CPU_BIG_ENDIAN),y)
arch/arm/lib/Makefile +1 -1
index b25c54585048..e10a769c72ec 100644
@@ -5,7 +5,7 @@
 # Copyright (C) 1995-2000 Russell King
 #
 
-lib-y          := backtrace.o changebit.o csumipv6.o csumpartial.o   \
+lib-y          := changebit.o csumipv6.o csumpartial.o               \
                   csumpartialcopy.o csumpartialcopyuser.o clearbit.o \
                   delay.o delay-loop.o findbit.o memchr.o memcpy.o   \
                   memmove.o memset.o setbit.o                        \

arch/arm/lib/Makefile lib-y := backtrace.o changebit.o csumipv6.o csumpartial.o \ · line 26

Quoted 8 lines
arch/arm/lib/Makefile +5 -0
@@ -19,6 +19,12 @@ lib-y                := backtrace.o changebit.o csumipv6.o csumpartial.o   \
 mmu-y          := clear_user.o copy_page.o getuser.o putuser.o       \
                   copy_from_user.o copy_to_user.o
 
+ifdef CONFIG_CC_IS_CLANG
+  lib-y += backtrace-clang.o
+else
+  lib-y += backtrace.o
+endif

The indentation should match the above (from this file). Looks like 1 tab after lib-y. See L34(CONFIG_CPU_32v3) for what I would have expected.

Quoted 13 lines
arch/arm/lib/Makefile +1 -0
+
 # using lib_ here won't override already available weak symbols
 obj-$(CONFIG_UACCESS_WITH_MEMCPY) += uaccess_with_memcpy.o
 
arch/arm/lib/backtrace-clang.S new file +3 -0
new file mode 100644
index 000000000000..2b02014dbdd1
@@ -0,0 +1,224 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ *  linux/arch/arm/lib/backtrace-clang.S

arch/arm/lib/backtrace-clang.S line 13

Quoted 10 lines
arch/arm/lib/backtrace-clang.S new file +10 -0
+ *
+ *  Copyright (C) 2019 Nathan Huckleberry
+ *
+ */
+#include <linux/kern_levels.h>
+#include <linux/linkage.h>
+#include <asm/assembler.h>
+               .text
+
+/* fp is 0 or stack frame */

ah, I see that the reference implementation uses an assembly comment here. Ok (sorry for the noise).

Quoted 11 lines
arch/arm/lib/backtrace-clang.S new file +11 -0
+
+#define frame  r4
+#define sv_fp  r5
+#define sv_pc  r6
+#define mask   r7
+#define sv_lr   r8
+
+ENTRY(c_backtrace)
+
+#if !defined(CONFIG_FRAME_POINTER) || !defined(CONFIG_PRINTK)
+               ret     lr

arch/arm/lib/backtrace-clang.S line 34

Quoted 10 lines
arch/arm/lib/backtrace-clang.S new file +10 -0
+ENDPROC(c_backtrace)
+#else
+
+
+/*
+ * Clang does not store pc or sp in function prologues
+ *             so we don't know exactly where the function
+ *             starts.
+ * We can treat the current frame's lr as the saved pc and the
+ *             preceding frame's lr as the lr, but we can't

preceding frame's lr as the current frame's lr, ...

arch/arm/lib/backtrace-clang.S line 46

Quoted 12 lines
arch/arm/lib/backtrace-clang.S new file +12 -0
+ *             trace the most recent call.
+ * Inserting a false stack frame allows us to reference the
+ *             function called last in the stacktrace.
+ * If the call instruction was a bl we can look at the callers
+ *             branch instruction to calculate the saved pc.
+ * We can recover the pc in most cases, but in cases such as
+ *             calling function pointers we cannot. In this
+ *             case, default to using the lr. This will be
+ *             some address in the function, but will not
+ *             be the function start.
+ * Unfortunately due to the stack frame layout we can't dump
+ *              r0 - r3, but these are less frequently saved.

The use of tabs vs spaces in these comments is inconsistent. Not that I can see whitespace, but:
https://github.com/nickdesaulniers/dotfiles/blob/37359525f5a403b4ed2d3f9d1bbbee2da8ec8115/.vimrc#L35-L41
Also, I don't think you need to tab indent every line after the first. Where did that format come from?

Quoted 22 lines
arch/arm/lib/backtrace-clang.S new file +22 -0
+ *
+ * Stack frame layout:
+ *             <larger addresses>
+ *             saved lr
+ *    frame => saved fp
+ *             optionally saved caller registers (r4 - r10)
+ *             optionally saved arguments (r0 - r3)
+ *             <top of stack frame>
+ *             <smaller addresses>
+ *
+ * Functions start with the following code sequence:
+ * corrected pc =>  stmfd sp!, {..., fp, lr}
+ *                 add fp, sp, #x
+ *                 stmfd sp!, {r0 - r3} (optional)
+ *
+ *
+ *
+ *
+ *
+ *
+ * The diagram below shows an example stack setup
+ *     for dump_stack.

arch/arm/lib/backtrace-clang.S line 78

Quoted 10 lines
arch/arm/lib/backtrace-clang.S new file +10 -0
+ *
+ * The frame for c_backtrace has pointers to the
+ *     code of dump_stack. This is why the frame of
+ *     c_backtrace is used to for the pc calculation
+ *     of dump_stack. This is why we must move back
+ *     a frame to print dump_stack.
+ *
+ * The stored locals for dump_stack are in dump_stack's
+ *     frame. This means that to fully print dump_stack's frame
+ *     we need the both the frame for dump_stack (for locals) and the

we need both the ...
(There's an extra the in the sentence).

Quoted 27 lines
arch/arm/lib/backtrace-clang.S new file +27 -0
+ *     frame that was called by dump_stack (for pc).
+ *
+ * To print locals we must know where the function start is. If
+ *     we read the function prologue opcodes we can determine
+ *     which variables are stored in the stack frame.
+ *
+ * To find the function start of dump_stack we can look at the
+ *     stored LR of show_stack. It points at the instruction
+ *     directly after the bl dump_stack. We can then read the
+ *     offset from the bl opcode to determine where the branch takes us.
+ *     The address calculated must be the start of dump_stack.
+ *
+ * c_backtrace frame           dump_stack:
+ * {[LR]    }  ============|   ...
+ * {[FP]    }  =======|    |   bl c_backtrace
+ *                    |    |=> ...
+ * {[R4-R10]}         |
+ * {[R0-R3] }         |        show_stack:
+ * dump_stack frame   |        ...
+ * {[LR]    } =============|   bl dump_stack
+ * {[FP]    } <=======|    |=> ...
+ * {[R4-R10]}
+ * {[R0-R3] }
+ */
+
+stmfd   sp!, {r4 - r9, fp, lr} @ Save an extra register
+                               @ to ensure 8 byte alignment
Quoted 8 lines

arch/arm/lib/backtrace-clang.S line 110

Quoted 5 lines
arch/arm/lib/backtrace-clang.S new file +5 -0
+movs   frame, r0               @ if frame pointer is zero
+beq    no_frame                @ we have no stack frames
+
+tst    r1, #0x10               @ 26 or 32-bit mode?
+moveq  mask, #0xfc000003

Should we be using different masks for ARM vs THUMB as per the reference implementation?

The change that introduces the arm/thumb code looked like a script that was run over all arm in the kernel. Neither this code nor the reference solution is compatible with arm, so there's no need for the change.

Quoted 15 lines
Quoted 15 lines
arch/arm/lib/backtrace-clang.S new file +14 -0
+movne  mask, #0                @ mask for 32-bit
+
+/*
+ * Switches the current frame to be the frame for dump_stack.
+ */
+               add     frame, sp, #24          @ switch to false frame
+for_each_frame:        tst     frame, mask             @ Check for address exceptions
+               bne     no_frame
+
+/*
+ * sv_fp is the stack frame with the locals for the current considered
+ *     function.
+ * sv_pc is the saved lr frame the frame above. This is a pointer to a
+ *     code address within the current considered function, but
Quoted 8 lines

arch/arm/lib/backtrace-clang.S line 129

Quoted 5 lines
arch/arm/lib/backtrace-clang.S new file +5 -0
+ *     it is not the function start. This value gets updated to be
+ *     the function start later if it is possible.
+ */
+1001:          ldr     sv_pc, [frame, #4]      @ get saved 'pc'
+1002:          ldr     sv_fp, [frame, #0]      @ get saved fp

The reference implementation applies the mask to sv_pc and sv_fp. I assume we want to, too?

The mask is already applied to both. See for_each_frame:

Quoted 110 lines
Quoted 12 lines
arch/arm/lib/backtrace-clang.S new file +11 -0
+
+               teq     sv_fp, #0               @ make sure next frame exists
+               beq     no_frame
+
+/*
+ * sv_lr is the lr from the function that called the current function. This
+ *     is a pointer to a code address in the current function's caller.
+ *     sv_lr-4 is the instruction used to call the current function.
+ * This sv_lr can be used to calculate the function start if the function
+ *     was called using a bl instruction. If the function start
+ *     can be recovered sv_pc is overwritten with the function start.

arch/arm/lib/backtrace-clang.S line 150

Quoted 10 lines
arch/arm/lib/backtrace-clang.S new file +10 -0
+ * If the current function was called using a function pointer we cannot
+ *     recover the function start and instead continue with sv_pc as
+ *     an arbitrary value within the current function. If this is the case
+ *     we cannot print registers for the current function, but the stacktrace
+ *     is still printed properly.
+ */
+1003:          ldr     sv_lr, [sv_fp, #4]      @ get saved lr from next frame
+
+               ldr     r0, [sv_lr, #-4]        @ get call instruction
+               ldr     r3, .Ldsi+8

I wonder what dsi stands for, it could use a better name. Maybe put that mask in a more descriptively named section and use that instead of .Ldsi+8?

arch/arm/lib/backtrace-clang.S line 156

Quoted 6 lines
arch/arm/lib/backtrace-clang.S new file +6 -0
+               and     r2, r3, r0              @ is this a bl call
+               teq     r2, r3
+               bne     finished_setup          @ give up if it's not
+               and     r0, #0xffffff           @ get call offset 24-bit int
+               lsl     r0, r0, #8              @ sign extend offset
+               asr     r0, r0, #8

It's too bad this should work for older ARM versions, v6 added dedicated instructions for this:
http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0489c/Cihfifdd.html

Quoted 25 lines
arch/arm/lib/backtrace-clang.S new file +25 -0
+               ldr     sv_pc, [sv_fp, #4]      @ get lr address
+               add     sv_pc, sv_pc, #-4       @ get call instruction address
+               add     sv_pc, sv_pc, #8        @ take care of prefetch
+               add     sv_pc, sv_pc, r0, lsl #2 @ find function start
+
+finished_setup:
+
+               bic     sv_pc, sv_pc, mask      @ mask PC/LR for the mode
+
+/*
+ * Print the function (sv_pc) and where it was called
+ *     from (sv_lr).
+ */
+1004:          mov     r0, sv_pc
+
+               mov     r1, sv_lr
+               mov     r2, frame
+               bic     r1, r1, mask            @ mask PC/LR for the mode
+               bl      dump_backtrace_entry
+
+/*
+ * Test if the function start is a stmfd instruction
+ *     to determine which registers were stored in the function
+ *     prologue.
+ * If we could not recover the sv_pc because we were called through

arch/arm/lib/backtrace-clang.S line 191

Quoted 10 lines
arch/arm/lib/backtrace-clang.S new file +10 -0
+ *     a function pointer the comparison will fail and no registers
+ *     will print.
+ */
+1005:          ldr     r1, [sv_pc, #0]         @ if stmfd sp!, {..., fp, lr}
+               ldr     r3, .Ldsi               @ instruction exists,
+               teq     r3, r1, lsr #11
+               ldr     r0, [frame]             @ locals are stored in
+                                               @ the preceding frame
+               subeq   r0, r0, #4
+               bleq    dump_backtrace_stm      @ dump saved registers

Do we need to do anything to test .Ldsi+4? Otherwise looks like we define it but don't use it?

Quoted 33 lines
arch/arm/lib/backtrace-clang.S new file +33 -0
+
+/*
+ * If we are out of frames or if the next frame
+ *     is invalid.
+ */
+               teq     sv_fp, #0               @ zero saved fp means
+               beq     no_frame                @ no further frames
+
+               cmp     sv_fp, frame            @ next frame must be
+               mov     frame, sv_fp            @ above the current frame
+               bhi     for_each_frame
+
+1006:          adr     r0, .Lbad
+               mov     r1, frame
+               bl      printk
+no_frame:      ldmfd   sp!, {r4 - r9, fp, pc}
+ENDPROC(c_backtrace)
+               .pushsection __ex_table,"a"
+               .align  3
+               .long   1001b, 1006b
+               .long   1002b, 1006b
+               .long   1003b, 1006b
+               .long   1004b, 1006b
+               .long   1005b, 1006b
+               .popsection
+
+.Lbad:         .asciz  "Backtrace aborted due to bad frame pointer <%p>\n"
+               .align
+.Ldsi:         .word   0xe92d4800 >> 11        @ stmfd sp!, {... fp, lr}
+               .word   0xe92d0000 >> 11        @ stmfd sp!, {}
+               .word   0x0b000000              @ bl if these bits are set
+
+#endif
Quoted 8 lines
Quoted 3 lines

--
2.23.0.rc1.153.gdeed80330f-goog

--
Thanks,
~Nick Desaulniers

Thanks for the review, will send a v2 with your suggestions.

#3 lore raw

PATCH v2 1/1 ARM: UNWINDER_FRAME_POINTER implementation for Clang

The stackframe setup when compiled with clang is different. Since the stack unwinder expects the gcc stackframe setup it fails to print backtraces. This patch adds support for the clang stackframe setup.

---
Changes from v1->v2
* Fix indentation in various files
* Swap spaces for tabs
* Rename Ldsi to Lopcode
* Remove unused Ldsi entry

 arch/arm/Kconfig.debug         |   2 +-
 arch/arm/Makefile              |   5 +-
 arch/arm/lib/Makefile          |   8 +-
 arch/arm/lib/backtrace-clang.S | 229 +++++++++++++++++++++++++++++++++
 4 files changed, 241 insertions(+), 3 deletions(-)
 create mode 100644 arch/arm/lib/backtrace-clang.S
arch/arm/Kconfig.debug +1 -1
index 85710e078afb..b9c674ec19e0 100644
@@ -56,7 +56,7 @@ choice
 
 config UNWINDER_FRAME_POINTER
 	bool "Frame pointer unwinder"
-	depends on !THUMB2_KERNEL && !CC_IS_CLANG
+	depends on !THUMB2_KERNEL
 	select ARCH_WANT_FRAME_POINTERS
 	select FRAME_POINTER
 	help
arch/arm/Makefile +4 -1
index c3624ca6c0bc..6f251c201db0 100644
@@ -36,7 +36,10 @@ KBUILD_CFLAGS	+= $(call cc-option,-mno-unaligned-access)
 endif
 
 ifeq ($(CONFIG_FRAME_POINTER),y)
-KBUILD_CFLAGS	+=-fno-omit-frame-pointer -mapcs -mno-sched-prolog
+KBUILD_CFLAGS	+=-fno-omit-frame-pointer
+ifeq ($(CONFIG_CC_IS_GCC),y)
+KBUILD_CFLAGS += -mapcs -mno-sched-prolog
+endif
 endif
 
 ifeq ($(CONFIG_CPU_BIG_ENDIAN),y)
arch/arm/lib/Makefile +7 -1
index b25c54585048..6d2ba454f25b 100644
@@ -5,7 +5,7 @@
 # Copyright (C) 1995-2000 Russell King
 #
 
-lib-y		:= backtrace.o changebit.o csumipv6.o csumpartial.o   \
+lib-y		:= changebit.o csumipv6.o csumpartial.o               \
 		   csumpartialcopy.o csumpartialcopyuser.o clearbit.o \
 		   delay.o delay-loop.o findbit.o memchr.o memcpy.o   \
 		   memmove.o memset.o setbit.o                        \
@@ -19,6 +19,12 @@ lib-y		:= backtrace.o changebit.o csumipv6.o csumpartial.o   \
 mmu-y		:= clear_user.o copy_page.o getuser.o putuser.o       \
 		   copy_from_user.o copy_to_user.o
 
+ifdef CONFIG_CC_IS_CLANG
+  lib-y	+= backtrace-clang.o
+else
+  lib-y	+= backtrace.o
+endif
+
 # using lib_ here won't override already available weak symbols
 obj-$(CONFIG_UACCESS_WITH_MEMCPY) += uaccess_with_memcpy.o
 
arch/arm/lib/backtrace-clang.S new file +229 -0

229 lines not on this page · raw message

Signature · 1 line
2.23.0.rc1.153.gdeed80330f-goog
Tested-by on v2 1/1
#4 lore raw
Quoted 107 lines · Nathan Huckleberry

The stackframe setup when compiled with clang is different. Since the stack unwinder expects the gcc stackframe setup it fails to print backtraces. This patch adds support for the clang stackframe setup.

---
Changes from v1->v2
* Fix indentation in various files
* Swap spaces for tabs
* Rename Ldsi to Lopcode
* Remove unused Ldsi entry

 arch/arm/Kconfig.debug         |   2 +-
 arch/arm/Makefile              |   5 +-
 arch/arm/lib/Makefile          |   8 +-
 arch/arm/lib/backtrace-clang.S | 229 +++++++++++++++++++++++++++++++++
 4 files changed, 241 insertions(+), 3 deletions(-)
 create mode 100644 arch/arm/lib/backtrace-clang.S
arch/arm/Kconfig.debug +1 -1
index 85710e078afb..b9c674ec19e0 100644
@@ -56,7 +56,7 @@ choice
 
 config UNWINDER_FRAME_POINTER
        bool "Frame pointer unwinder"
-       depends on !THUMB2_KERNEL && !CC_IS_CLANG
+       depends on !THUMB2_KERNEL
        select ARCH_WANT_FRAME_POINTERS
        select FRAME_POINTER
        help
arch/arm/Makefile +4 -1
index c3624ca6c0bc..6f251c201db0 100644
@@ -36,7 +36,10 @@ KBUILD_CFLAGS        += $(call cc-option,-mno-unaligned-access)
 endif
 
 ifeq ($(CONFIG_FRAME_POINTER),y)
-KBUILD_CFLAGS  +=-fno-omit-frame-pointer -mapcs -mno-sched-prolog
+KBUILD_CFLAGS  +=-fno-omit-frame-pointer
+ifeq ($(CONFIG_CC_IS_GCC),y)
+KBUILD_CFLAGS += -mapcs -mno-sched-prolog
+endif
 endif
 
 ifeq ($(CONFIG_CPU_BIG_ENDIAN),y)
arch/arm/lib/Makefile +7 -1
index b25c54585048..6d2ba454f25b 100644
@@ -5,7 +5,7 @@
 # Copyright (C) 1995-2000 Russell King
 #
 
-lib-y          := backtrace.o changebit.o csumipv6.o csumpartial.o   \
+lib-y          := changebit.o csumipv6.o csumpartial.o               \
                   csumpartialcopy.o csumpartialcopyuser.o clearbit.o \
                   delay.o delay-loop.o findbit.o memchr.o memcpy.o   \
                   memmove.o memset.o setbit.o                        \
@@ -19,6 +19,12 @@ lib-y                := backtrace.o changebit.o csumipv6.o csumpartial.o   \
 mmu-y          := clear_user.o copy_page.o getuser.o putuser.o       \
                   copy_from_user.o copy_to_user.o
 
+ifdef CONFIG_CC_IS_CLANG
+  lib-y        += backtrace-clang.o
+else
+  lib-y        += backtrace.o
+endif
+
 # using lib_ here won't override already available weak symbols
 obj-$(CONFIG_UACCESS_WITH_MEMCPY) += uaccess_with_memcpy.o
 
arch/arm/lib/backtrace-clang.S new file +22 -0
new file mode 100644
index 000000000000..6f2a8a57d0fb
@@ -0,0 +1,229 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ *  linux/arch/arm/lib/backtrace-clang.S
+ *
+ *  Copyright (C) 2019 Nathan Huckleberry
+ *
+ */
+#include <linux/kern_levels.h>
+#include <linux/linkage.h>
+#include <asm/assembler.h>
+               .text
+
+/* fp is 0 or stack frame */
+
+#define frame  r4
+#define sv_fp  r5
+#define sv_pc  r6
+#define mask   r7
+#define sv_lr  r8
+
+ENTRY(c_backtrace)
+

arch/arm/lib/backtrace-clang.S line 32

Quoted 10 lines
arch/arm/lib/backtrace-clang.S new file +10 -0
+#if !defined(CONFIG_FRAME_POINTER) || !defined(CONFIG_PRINTK)
+               ret     lr
+ENDPROC(c_backtrace)
+#else
+
+
+/*
+ * Clang does not store pc or sp in function prologues
+ * so we don't know exactly where the function
+ * starts.

To quickly re-wrap text (if you're using vim) such as with comments like these:
shift+v (VISUAL LINE MODE)
j or k to highlight lines
gq (to rewrap)
You may need set cc=80 (not sure).

Quoted 7 lines
arch/arm/lib/backtrace-clang.S new file +7 -0
+ *
+ * We can treat the current frame's lr as the saved pc and the
+ * preceding frame's lr as the current frame's lr,
+ * but we can't trace the most recent call.
+ * Inserting a false stack frame allows us to reference the
+ * function called last in the stacktrace.
+ *

arch/arm/lib/backtrace-clang.S line 49

Quoted 10 lines
arch/arm/lib/backtrace-clang.S new file +10 -0
+ * If the call instruction was a bl we can look at the callers
+ * branch instruction to calculate the saved pc.
+ * We can recover the pc in most cases, but in cases such as
+ * calling function pointers we cannot. In this
+ * case, default to using the lr. This will be
+ * some address in the function, but will not
+ * be the function start.
+ *
+ * Unfortunately due to the stack frame layout we can't dump
+ *              r0 - r3, but these are less frequently saved.

I guess if they were spilled, but I'm ok with this; I'd rather have a working unwinder than disabled config. The printing is a debug feature that's nice to have, but the main focus should be unwinding. We can always revisit improving support.

Quoted 47 lines
arch/arm/lib/backtrace-clang.S new file +47 -0
+ *
+ * Stack frame layout:
+ *             <larger addresses>
+ *             saved lr
+ *     frame=> saved fp
+ *             optionally saved caller registers (r4 - r10)
+ *             optionally saved arguments (r0 - r3)
+ *             <top of stack frame>
+ *             <smaller addresses>
+ *
+ * Functions start with the following code sequence:
+ * corrected pc =>  stmfd sp!, {..., fp, lr}
+ *             add fp, sp, #x
+ *             stmfd sp!, {r0 - r3} (optional)
+ *
+ *
+ *
+ *
+ *
+ *
+ * The diagram below shows an example stack setup
+ * for dump_stack.
+ *
+ * The frame for c_backtrace has pointers to the
+ * code of dump_stack. This is why the frame of
+ * c_backtrace is used to for the pc calculation
+ * of dump_stack. This is why we must move back
+ * a frame to print dump_stack.
+ *
+ * The stored locals for dump_stack are in dump_stack's
+ * frame. This means that to fully print dump_stack's frame
+ * we need both the frame for dump_stack (for locals) and the
+ * frame that was called by dump_stack (for pc).
+ *
+ * To print locals we must know where the function start is. If
+ * we read the function prologue opcodes we can determine
+ * which variables are stored in the stack frame.
+ *
+ * To find the function start of dump_stack we can look at the
+ * stored LR of show_stack. It points at the instruction
+ * directly after the bl dump_stack. We can then read the
+ * offset from the bl opcode to determine where the branch takes us.
+ * The address calculated must be the start of dump_stack.
+ *
+ * c_backtrace frame           dump_stack:
+ * {[LR]    }  ============|   ...
+ * {[FP]    }  =======|    |   bl c_backtrace

arch/arm/lib/backtrace-clang.S line 106

Quoted 10 lines
arch/arm/lib/backtrace-clang.S new file +10 -0
+ *                    |    |=> ...
+ * {[R4-R10]}         |
+ * {[R0-R3] }         |        show_stack:
+ * dump_stack frame   |        ...
+ * {[LR]    } =============|   bl dump_stack
+ * {[FP]    } <=======|    |=> ...
+ * {[R4-R10]}
+ * {[R0-R3] }
+ */
+

===>

arch/arm/lib/backtrace-clang.S line 114

Quoted 8 lines
arch/arm/lib/backtrace-clang.S new file +8 -0
+stmfd   sp!, {r4 - r9, fp, lr} @ Save an extra register
+                               @ to ensure 8 byte alignment
+movs   frame, r0               @ if frame pointer is zero
+beq    no_frame                @ we have no stack frames
+
+tst    r1, #0x10               @ 26 or 32-bit mode?
+moveq  mask, #0xfc000003
+movne  mask, #0                @ mask for 32-bit

<== this section of the patch has weird indentation. The rest uses 2 tabs, this has none.

Quoted 66 lines
arch/arm/lib/backtrace-clang.S new file +66 -0
+
+/*
+ * Switches the current frame to be the frame for dump_stack.
+ */
+               add     frame, sp, #24          @ switch to false frame
+for_each_frame:        tst     frame, mask             @ Check for address exceptions
+               bne     no_frame
+
+/*
+ * sv_fp is the stack frame with the locals for the current considered
+ * function.
+ *
+ * sv_pc is the saved lr frame the frame above. This is a pointer to a
+ * code address within the current considered function, but
+ * it is not the function start. This value gets updated to be
+ * the function start later if it is possible.
+ */
+1001:          ldr     sv_pc, [frame, #4]      @ get saved 'pc'
+1002:          ldr     sv_fp, [frame, #0]      @ get saved fp
+
+               teq     sv_fp, mask             @ make sure next frame exists
+               beq     no_frame
+
+/*
+ * sv_lr is the lr from the function that called the current function. This
+ * is a pointer to a code address in the current function's caller.
+ * sv_lr-4 is the instruction used to call the current function.
+ *
+ * This sv_lr can be used to calculate the function start if the function
+ * was called using a bl instruction. If the function start
+ * can be recovered sv_pc is overwritten with the function start.
+ *
+ * If the current function was called using a function pointer we cannot
+ * recover the function start and instead continue with sv_pc as
+ * an arbitrary value within the current function. If this is the case
+ * we cannot print registers for the current function, but the stacktrace
+ * is still printed properly.
+ */
+1003:          ldr     sv_lr, [sv_fp, #4]      @ get saved lr from next frame
+
+               ldr     r0, [sv_lr, #-4]        @ get call instruction
+               ldr     r3, .Lopcode+4
+               and     r2, r3, r0              @ is this a bl call
+               teq     r2, r3
+               bne     finished_setup          @ give up if it's not
+               and     r0, #0xffffff           @ get call offset 24-bit int
+               lsl     r0, r0, #8              @ sign extend offset
+               asr     r0, r0, #8
+               ldr     sv_pc, [sv_fp, #4]      @ get lr address
+               add     sv_pc, sv_pc, #-4       @ get call instruction address
+               add     sv_pc, sv_pc, #8        @ take care of prefetch
+               add     sv_pc, sv_pc, r0, lsl #2@ find function start
+
+finished_setup:
+
+               bic     sv_pc, sv_pc, mask      @ mask PC/LR for the mode
+
+/*
+ * Print the function (sv_pc) and where it was called
+ * from (sv_lr).
+ */
+1004:          mov     r0, sv_pc
+
+               mov     r1, sv_lr
+               mov     r2, frame
+               bic     r1, r1, mask            @ mask PC/LR for the mode

arch/arm/lib/backtrace-clang.S line 190

Quoted 10 lines
arch/arm/lib/backtrace-clang.S new file +10 -0
+               bl      dump_backtrace_entry
+
+/*
+ * Test if the function start is a stmfd instruction
+ * to determine which registers were stored in the function
+ * prologue.
+ *
+ * If we could not recover the sv_pc because we were called through
+ * a function pointer the comparison will fail and no registers
+ * will print.

Will we still unwind though?

Quoted 40 lines
arch/arm/lib/backtrace-clang.S new file +39 -1
+ */
+1005:          ldr     r1, [sv_pc, #0]         @ if stmfd sp!, {..., fp, lr}
+               ldr     r3, .Lopcode            @ instruction exists,
+               teq     r3, r1, lsr #11
+               ldr     r0, [frame]             @ locals are stored in
+                                               @ the preceding frame
+               subeq   r0, r0, #4
+               bleq    dump_backtrace_stm      @ dump saved registers
+
+/*
+ * If we are out of frames or if the next frame is invalid.
+ */
+               teq     sv_fp, #0               @ zero saved fp means
+               beq     no_frame                @ no further frames
+
+               cmp     sv_fp, frame            @ next frame must be
+               mov     frame, sv_fp            @ above the current frame
+               bhi     for_each_frame
+
+1006:          adr     r0, .Lbad
+               mov     r1, frame
+               bl      printk
+no_frame:      ldmfd   sp!, {r4 - r9, fp, pc}
+ENDPROC(c_backtrace)
+               .pushsection __ex_table,"a"
+               .align  3
+               .long   1001b, 1006b
+               .long   1002b, 1006b
+               .long   1003b, 1006b
+               .long   1004b, 1006b
+               .long   1005b, 1006b
+               .popsection
+
+.Lbad:         .asciz  "Backtrace aborted due to bad frame pointer <%p>\n"
+               .align
+.Lopcode:      .word   0xe92d4800 >> 11        @ stmfd sp!, {... fp, lr}
+               .word   0x0b000000              @ bl if these bits are set
+
+#endif
--
Quoted 2 lines

2.23.0.rc1.153.gdeed80330f-goog

OK, with you patch applied on today's Linux next,
CONFIG_UNWINDER_FRAME_POINTER, and ToT Clang:

$ qemu-system-arm -kernel arch/arm/boot/zImage -nographic -m 2048
--append "console=ttyAMA0 root=/dev/ram0" -machine virt
[    0.000000] Linux version 5.3.0-rc5-07709-gac2d7d4a10c1-dirty
([email protected]) (clang version 10.0.0
(https://github.com/llvm/llvm-project.git
da648ab8de3638ff82d6b9349c603b854a0224d6)) #53 SMP Wed Aug 21 20:05:15
PDT 2019
...
[    0.957046] Kernel panic - not syncing: VFS: Unable to mount root
fs on unknown-block(1,0)
[    0.957490] CPU: 0 PID: 1 Comm: swapper/0 Not tainted
5.3.0-rc5-07709-gac2d7d4a10c1-dirty #53
[    0.957808] Hardware name: Generic DT based system
[    0.958046] Backtrace:
[    0.958504] [<c030da98>] (dump_backtrace) from [<c030da94>]
(show_stack+0x14/0x18)
[    0.958844]  r10:c16f585c r6:00000000 r5:c198c7e4 r4:600000d3
[    0.959085] [<c030da80>] (show_stack) from [<c106c2c8>]
(dump_stack+0xac/0xd8)
[    0.959358] [<c106c21c>] (dump_stack) from [<c03504a0>] (panic+0x118/0x354)
[    0.959568]  r5:c19a61b5 r4:c1427ce5
[    0.959722] [<c0350388>] (panic) from [<c1601624>]
(mount_block_root+0x13c/0x1f0)
[    0.959947] [<c16014e8>] (mount_block_root) from [<c1601a48>]
(mount_root+0xb0/0xb4)
[    0.960210]  r10:00000000 r9:00000000 r8:00000000 r7:00000000
r6:00000000 r5:00100000
[    0.960492]  r4:c1427d49
[    0.960600] [<c1601998>] (mount_root) from [<c1601d0c>]
(prepare_namespace+0x1ec/0x1f0)
[    0.960886]  r5:c19a3d30 r4:c16f5868
[    0.961021] [<c1601b20>] (prepare_namespace) from [<c16011ac>]
(kernel_init_freeable+0xe0/0xf4)
[    0.961330]  r5:00000000 r4:c19a3d1c
[    0.961468] [<c16010cc>] (kernel_init_freeable) from [<c10868d8>]
(kernel_init+0xc/0x2ac)
[    0.961761]  r5:c10868cc r4:00000000
[    0.961913] [<c10868d8>] (kernel_init) from [<c03010e8>]
(ret_from_fork+0x14/0x2c)
[    0.962210] Exception stack(0xea09bf94 to 0xea09bfdc)
[    0.962490] bf80:
c10868d8 00000000 c10868cc
[    0.962883] bfa0: 00000000 00000000 00000000 c03010e8 00000000
00000000 00000000 00000000
[    0.963202] bfc0: 00000000 00000000 00000000 00000000 00000000
00000000 00000000
[    0.963936] ---[ end Kernel panic - not syncing: VFS: Unable to
mount root fs on unknown-block(1,0) ]---

For comparison, the reference implementation:

[    0.000000] Linux version 5.3.0-rc5-07709-gac2d7d4a10c1-dirty
([email protected]) (gcc version 8.2.0
(Debian 8.2.0-14+build1)) #54 SMP Wed Aug 21 20:15:27 PDT 2019
...
[    1.048134] Kernel panic - not syncing: VFS: Unable to mount root
fs on unknown-block(1,0)
[    1.048617] CPU: 0 PID: 1 Comm: swapper/0 Not tainted
5.3.0-rc5-07709-gac2d7d4a10c1-dirty #54
[    1.048935] Hardware name: Generic DT based system
[    1.049191] Backtrace:
[    1.049663] [<c030dff8>] (dump_backtrace) from [<c030e368>]
(show_stack+0x18/0x1c)
[    1.050038]  r7:c16ed85c r6:600000d3 r5:00000000 r4:c198f804
[    1.050292] [<c030e350>] (show_stack) from [<c0f550bc>]
(dump_stack+0xbc/0xd0)
[    1.050581] [<c0f55000>] (dump_stack) from [<c0349fe8>] (panic+0x118/0x330)
[    1.050834]  r7:c16ed85c r6:c13404fc r5:00000000 r4:c19a5870
[    1.051078] [<c0349ed4>] (panic) from [<c16017dc>]
(mount_block_root+0x264/0x284)
[    1.051344]  r3:0ed1c175 r2:0ed1c175 r1:ea09be84 r0:c13404fc
[    1.051553]  r7:c16ed85c
[    1.051661] [<c1601578>] (mount_block_root) from [<c1601a64>]
(mount_root+0x124/0x140)
[    1.051936]  r10:ffffe000 r9:c16ed858 r8:c19a3400 r7:c1809100
r6:00000008 r5:c1804c48
[    1.052238]  r4:00100000
[    1.052351] [<c1601940>] (mount_root) from [<c1601c04>]
(prepare_namespace+0x184/0x1cc)
[    1.052638]  r10:ffffe000 r9:c16ed858 r8:c19a3400 r7:c19a3400
r6:00000008 r5:c19a3430
[    1.052915]  r4:c16ed85c
[    1.053025] [<c1601a80>] (prepare_namespace) from [<c1601308>]
(kernel_init_freeable+0x2f8/0x308)
[    1.053337]  r6:00000008 r5:c177b7c0 r4:c16ed838
[    1.053507] [<c1601010>] (kernel_init_freeable) from [<c0f6ceb0>]
(kernel_init+0x10/0x118)
[    1.053798]  r10:00000000 r9:00000000 r8:00000000 r7:00000000
r6:00000000 r5:c0f6cea0
[    1.054083]  r4:00000000
[    1.054190] [<c0f6cea0>] (kernel_init) from [<c03010e8>]
(ret_from_fork+0x14/0x2c)
[    1.054501] Exception stack(0xea09bfb0 to 0xea09bff8)
[    1.054792] bfa0:                                     00000000
00000000 00000000 00000000
[    1.055169] bfc0: 00000000 00000000 00000000 00000000 00000000
00000000 00000000 00000000
[    1.055526] bfe0: 00000000 00000000 00000000 00000000 00000013 00000000
[    1.055795]  r5:c0f6cea0 r4:00000000
[    1.056410] ---[ end Kernel panic - not syncing: VFS: Unable to

mount root fs on unknown-block(1,0) ]---

So the stack traces look comparable (same unwind "path"). Looks like GCC spilled r0-r3 in panic(), but not much else. I guess Clang could have spilled these anywhere and we simply wont be able to print them. Maybe making the comment about this in you patch ALL CAPS might draw attention to it in case someone ever notices a difference between the unwind printout and the disassembly, but I assume that's unlikely, but I also don't know if this functionality is relied upon heavily for debugging.

In that sense:

(you can carry that on to v3)
With the above suggestions, I'd be happy to then add my reviewed by tag. Thanks for all of the work that went into this.

Signature · 2 lines
Thanks,
~Nick Desaulniers
#5 lore raw

PATCH v3 1/1 ARM: UNWINDER_FRAME_POINTER implementation for Clang

The stackframe setup when compiled with clang is different. Since the stack unwinder expects the gcc stackframe setup it fails to print backtraces. This patch adds support for the clang stackframe setup.

---
Changes from v2->v3
* Fix indentation on code
* Fix comment formatting

 arch/arm/Kconfig.debug         |   2 +-
 arch/arm/Makefile              |   5 +-
 arch/arm/lib/Makefile          |   8 +-
 arch/arm/lib/backtrace-clang.S | 217 +++++++++++++++++++++++++++++++++
 4 files changed, 229 insertions(+), 3 deletions(-)
 create mode 100644 arch/arm/lib/backtrace-clang.S
arch/arm/Kconfig.debug +1 -1
index 85710e078afb..b9c674ec19e0 100644
@@ -56,7 +56,7 @@ choice
 
 config UNWINDER_FRAME_POINTER
 	bool "Frame pointer unwinder"
-	depends on !THUMB2_KERNEL && !CC_IS_CLANG
+	depends on !THUMB2_KERNEL
 	select ARCH_WANT_FRAME_POINTERS
 	select FRAME_POINTER
 	help
arch/arm/Makefile +4 -1
index c3624ca6c0bc..6f251c201db0 100644
@@ -36,7 +36,10 @@ KBUILD_CFLAGS	+= $(call cc-option,-mno-unaligned-access)
 endif
 
 ifeq ($(CONFIG_FRAME_POINTER),y)
-KBUILD_CFLAGS	+=-fno-omit-frame-pointer -mapcs -mno-sched-prolog
+KBUILD_CFLAGS	+=-fno-omit-frame-pointer
+ifeq ($(CONFIG_CC_IS_GCC),y)
+KBUILD_CFLAGS += -mapcs -mno-sched-prolog
+endif
 endif
 
 ifeq ($(CONFIG_CPU_BIG_ENDIAN),y)
arch/arm/lib/Makefile +7 -1
index b25c54585048..6d2ba454f25b 100644
@@ -5,7 +5,7 @@
 # Copyright (C) 1995-2000 Russell King
 #
 
-lib-y		:= backtrace.o changebit.o csumipv6.o csumpartial.o   \
+lib-y		:= changebit.o csumipv6.o csumpartial.o               \
 		   csumpartialcopy.o csumpartialcopyuser.o clearbit.o \
 		   delay.o delay-loop.o findbit.o memchr.o memcpy.o   \
 		   memmove.o memset.o setbit.o                        \
@@ -19,6 +19,12 @@ lib-y		:= backtrace.o changebit.o csumipv6.o csumpartial.o   \
 mmu-y		:= clear_user.o copy_page.o getuser.o putuser.o       \
 		   copy_from_user.o copy_to_user.o
 
+ifdef CONFIG_CC_IS_CLANG
+  lib-y	+= backtrace-clang.o
+else
+  lib-y	+= backtrace.o
+endif
+
 # using lib_ here won't override already available weak symbols
 obj-$(CONFIG_UACCESS_WITH_MEMCPY) += uaccess_with_memcpy.o
 
arch/arm/lib/backtrace-clang.S new file +217 -0

217 lines not on this page · raw message

Signature · 1 line
2.23.0.187.g17f5b7556c-goog
Reviewed-by on v3 1/1
#6 lore raw
Quoted 11 lines · Nathan Huckleberry

The stackframe setup when compiled with clang is different. Since the stack unwinder expects the gcc stackframe setup it fails to print backtraces. This patch adds support for the clang stackframe setup.

Great, thanks for following up on the suggestions from code review. Since this is going to go up via the arm tree, which has its own process, please add my:

to the commit message, then submit the patch to the maintainer's patch tracking system:
https://www.armlinux.org.uk/developer/patches/info.php
(create a login, sign in, then visit:
https://www.armlinux.org.uk/developer/patches/add.php . I think the correct thing is to put the first line of the commit in the summary field, next/master as the kernel version (I applied/tested off of -next), then the rest of the commit message body in the Patch Notes field. Make sure to attach the patch file. Finally, it should appear at https://www.armlinux.org.uk/developer/patches/section.php?section=0
I think within 24hrs).

Quoted 293 lines

---
Changes from v2->v3
* Fix indentation on code
* Fix comment formatting

 arch/arm/Kconfig.debug         |   2 +-
 arch/arm/Makefile              |   5 +-
 arch/arm/lib/Makefile          |   8 +-
 arch/arm/lib/backtrace-clang.S | 217 +++++++++++++++++++++++++++++++++
 4 files changed, 229 insertions(+), 3 deletions(-)
 create mode 100644 arch/arm/lib/backtrace-clang.S
arch/arm/Kconfig.debug +1 -1
index 85710e078afb..b9c674ec19e0 100644
@@ -56,7 +56,7 @@ choice
 
 config UNWINDER_FRAME_POINTER
        bool "Frame pointer unwinder"
-       depends on !THUMB2_KERNEL && !CC_IS_CLANG
+       depends on !THUMB2_KERNEL
        select ARCH_WANT_FRAME_POINTERS
        select FRAME_POINTER
        help
arch/arm/Makefile +4 -1
index c3624ca6c0bc..6f251c201db0 100644
@@ -36,7 +36,10 @@ KBUILD_CFLAGS        += $(call cc-option,-mno-unaligned-access)
 endif
 
 ifeq ($(CONFIG_FRAME_POINTER),y)
-KBUILD_CFLAGS  +=-fno-omit-frame-pointer -mapcs -mno-sched-prolog
+KBUILD_CFLAGS  +=-fno-omit-frame-pointer
+ifeq ($(CONFIG_CC_IS_GCC),y)
+KBUILD_CFLAGS += -mapcs -mno-sched-prolog
+endif
 endif
 
 ifeq ($(CONFIG_CPU_BIG_ENDIAN),y)
arch/arm/lib/Makefile +7 -1
index b25c54585048..6d2ba454f25b 100644
@@ -5,7 +5,7 @@
 # Copyright (C) 1995-2000 Russell King
 #
 
-lib-y          := backtrace.o changebit.o csumipv6.o csumpartial.o   \
+lib-y          := changebit.o csumipv6.o csumpartial.o               \
                   csumpartialcopy.o csumpartialcopyuser.o clearbit.o \
                   delay.o delay-loop.o findbit.o memchr.o memcpy.o   \
                   memmove.o memset.o setbit.o                        \
@@ -19,6 +19,12 @@ lib-y                := backtrace.o changebit.o csumipv6.o csumpartial.o   \
 mmu-y          := clear_user.o copy_page.o getuser.o putuser.o       \
                   copy_from_user.o copy_to_user.o
 
+ifdef CONFIG_CC_IS_CLANG
+  lib-y        += backtrace-clang.o
+else
+  lib-y        += backtrace.o
+endif
+
 # using lib_ here won't override already available weak symbols
 obj-$(CONFIG_UACCESS_WITH_MEMCPY) += uaccess_with_memcpy.o
 
arch/arm/lib/backtrace-clang.S new file +217 -1
new file mode 100644
index 000000000000..2ff375144b55
@@ -0,0 +1,217 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ *  linux/arch/arm/lib/backtrace-clang.S
+ *
+ *  Copyright (C) 2019 Nathan Huckleberry
+ *
+ */
+#include <linux/kern_levels.h>
+#include <linux/linkage.h>
+#include <asm/assembler.h>
+               .text
+
+/* fp is 0 or stack frame */
+
+#define frame  r4
+#define sv_fp  r5
+#define sv_pc  r6
+#define mask   r7
+#define sv_lr  r8
+
+ENTRY(c_backtrace)
+
+#if !defined(CONFIG_FRAME_POINTER) || !defined(CONFIG_PRINTK)
+               ret     lr
+ENDPROC(c_backtrace)
+#else
+
+
+/*
+ * Clang does not store pc or sp in function prologues so we don't know exactly
+ * where the function starts.
+ *
+ * We can treat the current frame's lr as the saved pc and the preceding
+ * frame's lr as the current frame's lr, but we can't trace the most recent
+ * call.  Inserting a false stack frame allows us to reference the function
+ * called last in the stacktrace.
+ *
+ * If the call instruction was a bl we can look at the callers branch
+ * instruction to calculate the saved pc.  We can recover the pc in most cases,
+ * but in cases such as calling function pointers we cannot. In this case,
+ * default to using the lr. This will be some address in the function, but will
+ * not be the function start.
+ *
+ * Unfortunately due to the stack frame layout we can't dump r0 - r3, but these
+ * are less frequently saved.
+ *
+ * Stack frame layout:
+ *             <larger addresses>
+ *             saved lr
+ *     frame=> saved fp
+ *             optionally saved caller registers (r4 - r10)
+ *             optionally saved arguments (r0 - r3)
+ *             <top of stack frame>
+ *             <smaller addresses>
+ *
+ * Functions start with the following code sequence:
+ * corrected pc =>  stmfd sp!, {..., fp, lr}
+ *             add fp, sp, #x
+ *             stmfd sp!, {r0 - r3} (optional)
+ *
+ *
+ *
+ *
+ *
+ *
+ * The diagram below shows an example stack setup for dump_stack.
+ *
+ * The frame for c_backtrace has pointers to the code of dump_stack. This is
+ * why the frame of c_backtrace is used to for the pc calculation of
+ * dump_stack. This is why we must move back a frame to print dump_stack.
+ *
+ * The stored locals for dump_stack are in dump_stack's frame. This means that
+ * to fully print dump_stack's frame we need both the frame for dump_stack (for
+ * locals) and the frame that was called by dump_stack (for pc).
+ *
+ * To print locals we must know where the function start is. If we read the
+ * function prologue opcodes we can determine which variables are stored in the
+ * stack frame.
+ *
+ * To find the function start of dump_stack we can look at the stored LR of
+ * show_stack. It points at the instruction directly after the bl dump_stack.
+ * We can then read the offset from the bl opcode to determine where the branch
+ * takes us.  The address calculated must be the start of dump_stack.
+ *
+ * c_backtrace frame           dump_stack:
+ * {[LR]    }  ============|   ...
+ * {[FP]    }  =======|    |   bl c_backtrace
+ *                    |    |=> ...
+ * {[R4-R10]}         |
+ * {[R0-R3] }         |        show_stack:
+ * dump_stack frame   |        ...
+ * {[LR]    } =============|   bl dump_stack
+ * {[FP]    } <=======|    |=> ...
+ * {[R4-R10]}
+ * {[R0-R3] }
+ */
+
+               stmfd   sp!, {r4 - r9, fp, lr}  @ Save an extra register
+                                               @ to ensure 8 byte alignment
+               movs    frame, r0               @ if frame pointer is zero
+               beq     no_frame                @ we have no stack frames
+               tst     r1, #0x10               @ 26 or 32-bit mode?
+               moveq   mask, #0xfc000003
+               movne   mask, #0                @ mask for 32-bit
+
+/*
+ * Switches the current frame to be the frame for dump_stack.
+ */
+               add     frame, sp, #24          @ switch to false frame
+for_each_frame:        tst     frame, mask             @ Check for address exceptions
+               bne     no_frame
+
+/*
+ * sv_fp is the stack frame with the locals for the current considered
+ * function.
+ *
+ * sv_pc is the saved lr frame the frame above. This is a pointer to a code
+ * address within the current considered function, but it is not the function
+ * start. This value gets updated to be the function start later if it is
+ * possible.
+ */
+1001:          ldr     sv_pc, [frame, #4]      @ get saved 'pc'
+1002:          ldr     sv_fp, [frame, #0]      @ get saved fp
+
+               teq     sv_fp, mask             @ make sure next frame exists
+               beq     no_frame
+
+/*
+ * sv_lr is the lr from the function that called the current function. This is
+ * a pointer to a code address in the current function's caller.  sv_lr-4 is
+ * the instruction used to call the current function.
+ *
+ * This sv_lr can be used to calculate the function start if the function was
+ * called using a bl instruction. If the function start can be recovered sv_pc
+ * is overwritten with the function start.
+ *
+ * If the current function was called using a function pointer we cannot
+ * recover the function start and instead continue with sv_pc as an arbitrary
+ * value within the current function. If this is the case we cannot print
+ * registers for the current function, but the stacktrace is still printed
+ * properly.
+ */
+1003:          ldr     sv_lr, [sv_fp, #4]      @ get saved lr from next frame
+
+               ldr     r0, [sv_lr, #-4]        @ get call instruction
+               ldr     r3, .Lopcode+4
+               and     r2, r3, r0              @ is this a bl call
+               teq     r2, r3
+               bne     finished_setup          @ give up if it's not
+               and     r0, #0xffffff           @ get call offset 24-bit int
+               lsl     r0, r0, #8              @ sign extend offset
+               asr     r0, r0, #8
+               ldr     sv_pc, [sv_fp, #4]      @ get lr address
+               add     sv_pc, sv_pc, #-4       @ get call instruction address
+               add     sv_pc, sv_pc, #8        @ take care of prefetch
+               add     sv_pc, sv_pc, r0, lsl #2@ find function start
+
+finished_setup:
+
+               bic     sv_pc, sv_pc, mask      @ mask PC/LR for the mode
+
+/*
+ * Print the function (sv_pc) and where it was called from (sv_lr).
+ */
+1004:          mov     r0, sv_pc
+
+               mov     r1, sv_lr
+               mov     r2, frame
+               bic     r1, r1, mask            @ mask PC/LR for the mode
+               bl      dump_backtrace_entry
+
+/*
+ * Test if the function start is a stmfd instruction to determine which
+ * registers were stored in the function prologue.
+ *
+ * If we could not recover the sv_pc because we were called through a function
+ * pointer the comparison will fail and no registers will print. Unwinding will
+ * continue as if there had been no registers stored in this frame.
+ */
+1005:          ldr     r1, [sv_pc, #0]         @ if stmfd sp!, {..., fp, lr}
+               ldr     r3, .Lopcode            @ instruction exists,
+               teq     r3, r1, lsr #11
+               ldr     r0, [frame]             @ locals are stored in
+                                               @ the preceding frame
+               subeq   r0, r0, #4
+               bleq    dump_backtrace_stm      @ dump saved registers
+
+/*
+ * If we are out of frames or if the next frame is invalid.
+ */
+               teq     sv_fp, #0               @ zero saved fp means
+               beq     no_frame                @ no further frames
+
+               cmp     sv_fp, frame            @ next frame must be
+               mov     frame, sv_fp            @ above the current frame
+               bhi     for_each_frame
+
+1006:          adr     r0, .Lbad
+               mov     r1, frame
+               bl      printk
+no_frame:      ldmfd   sp!, {r4 - r9, fp, pc}
+ENDPROC(c_backtrace)
+               .pushsection __ex_table,"a"
+               .align  3
+               .long   1001b, 1006b
+               .long   1002b, 1006b
+               .long   1003b, 1006b
+               .long   1004b, 1006b
+               .long   1005b, 1006b
+               .popsection
+
+.Lbad:         .asciz  "Backtrace aborted due to bad frame pointer <%p>\n"
+               .align
+.Lopcode:      .word   0xe92d4800 >> 11        @ stmfd sp!, {... fp, lr}
+               .word   0x0b000000              @ bl if these bits are set
+
+#endif
--

2.23.0.187.g17f5b7556c-goog

Signature · 2 lines
Thanks,
~Nick Desaulniers
#7 lore raw
Quoted 17 lines · Nick Desaulniers' via Clang Built Linux
Quoted 11 lines · Nathan Huckleberry

The stackframe setup when compiled with clang is different. Since the stack unwinder expects the gcc stackframe setup it fails to print backtraces. This patch adds support for the clang stackframe setup.

Great, thanks for following up on the suggestions from code review. Since this is going to go up via the arm tree, which has its own process, please add my:

Quoted 11 lines

to the commit message, then submit the patch to the maintainer's patch tracking system:
https://www.armlinux.org.uk/developer/patches/info.php
(create a login, sign in, then visit:
https://www.armlinux.org.uk/developer/patches/add.php . I think the correct thing is to put the first line of the commit in the summary field, next/master as the kernel version (I applied/tested off of -next), then the rest of the commit message body in the Patch Notes field. Make sure to attach the patch file. Finally, it should appear at https://www.armlinux.org.uk/developer/patches/section.php?section=0
I think within 24hrs).

Also for the record, I came across this a couple of months ago, Catalin Marinas had a git config alias that could be used for sending one patch to the patches email address so that you don't have to muck around the web interface:

https://lore.kernel.org/lkml/[email protected]/

Web interface works fine but I prefer everything via the command line :)

Cheers,
Nathan

#8 lore raw
In reply to Nathan Huckleberry
Quoted 5 lines · Nathan Huckleberry
Quoted 2 lines · Nick Desaulniers

On Tue, Aug 20, 2019 at 12:44 PM Nathan Huckleberry <[email protected]> wrote:

...snip...

Quoted 9 lines
Quoted 5 lines

arch/arm/lib/backtrace-clang.S line 110

Quoted 2 lines
arch/arm/lib/backtrace-clang.S new file +2 -0
+tst    r1, #0x10               @ 26 or 32-bit mode?
+moveq  mask, #0xfc000003

Should we be using different masks for ARM vs THUMB as per the reference implementation?

The change that introduces the arm/thumb code looked like a script that was run over all arm in the kernel. Neither this code nor the reference solution is compatible with arm, so there's no need for the change.

Looks like you're referring to commit 8b592783a2e8 ("Thumb-2:
Implement the unified arch/arm/lib functions").

Currently, arch/arm/Kconfig.debug has:
57 config UNWINDER_FRAME_POINTER
58 bool "Frame pointer unwinder"
59 depends on !THUMB2_KERNEL && !CC_IS_CLANG

So it looks like UNWINDER_FRAME_POINTER and THUMB2_KERNEL are mutually exclusive. Probably could send a patch cleaning that up. (ie. removing the different masks; essentially removing the hunk from arch/arm/lib/backtrace.S from 8b592783a2e8).

Quoted 16 lines
Quoted 8 lines
Quoted 8 lines
arch/arm/lib/backtrace-clang.S new file +8 -0
+for_each_frame:        tst     frame, mask             @ Check for address exceptions
+               bne     no_frame
+
+/*
+ * sv_fp is the stack frame with the locals for the current considered
+ *     function.
+ * sv_pc is the saved lr frame the frame above. This is a pointer to a
+ *     code address within the current considered function, but
Quoted 8 lines
Quoted 5 lines

+ * it is not the function start. This value gets updated to be
+ * the function start later if it is possible.
+ */

+1001:          ldr     sv_pc, [frame, #4]      @ get saved 'pc'
+1002:          ldr     sv_fp, [frame, #0]      @ get saved fp

The reference implementation applies the mask to sv_pc and sv_fp. I assume we want to, too?

Quoted 1 line

The mask is already applied to both. See for_each_frame:

ah, under the finished_setup label.

Signature · 7 lines
Thanks,
~Nick Desaulniers

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel