[RFC,4/5] x86: Add SFrame support for x86 architecture

Message ID 20250318130333.18829-5-claudiu.zissulescu-ianculescu@oracle.com (mailing list archive)
State New
Headers
Series glibc: Add SFrame support for stack backtracking |

Checks

Context Check Description
redhat-pt-bot/TryBot-apply_patch success Patch applied to master at the time it was sent
linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 fail Patch failed to apply
linaro-tcwg-bot/tcwg_glibc_build--master-arm fail Patch failed to apply

Commit Message

Claudiu Zissulescu-Ianculescu March 18, 2025, 1:03 p.m. UTC
  From: Claudiu Zissulescu <claudiu.zissulescu-ianculescu@oracle.com>

The SFrame is well supported by x86 architecture since binutils 2.41.
Enable it to be used as default frame tracer.

Signed-off-by: Claudiu Zissulescu <claudiu.zissulescu-ianculescu@oracle.com>
---
 sysdeps/unix/sysv/linux/x86/uw-sigframe.h | 99 +++++++++++++++++++++++
 sysdeps/x86/bits/dl_find_object.h         |  2 +
 2 files changed, 101 insertions(+)
 create mode 100644 sysdeps/unix/sysv/linux/x86/uw-sigframe.h
  

Comments

Jens Remus March 19, 2025, 4:05 p.m. UTC | #1
On 18.03.2025 14:03, claudiu.zissulescu-ianculescu@oracle.com wrote:
> From: Claudiu Zissulescu <claudiu.zissulescu-ianculescu@oracle.com>
> 
> The SFrame is well supported by x86 architecture since binutils 2.41.
> Enable it to be used as default frame tracer.
> 
> Signed-off-by: Claudiu Zissulescu <claudiu.zissulescu-ianculescu@oracle.com>

> diff --git a/sysdeps/unix/sysv/linux/x86/uw-sigframe.h b/sysdeps/unix/sysv/linux/x86/uw-sigframe.h

> +#else /* ifdef __x86_64__  */

Maybe the following is less confusing:

#else /* !__x86_64__  */

> +
> +#define MD_DECODE_SIGNAL_FRAME x86_decode_signal_frame
> +
> +static _Unwind_Reason_Code
> +x86_fallback_frame_state (frame *frame)

I could not find any further reference to x86_fallback_frame_state.

> +{

Regards,
Jens
  
Claudiu Zissulescu-Ianculescu March 25, 2025, 11:29 a.m. UTC | #2
On 3/19/25 6:05 PM, Jens Remus wrote:
> On 18.03.2025 14:03, claudiu.zissulescu-ianculescu@oracle.com wrote:
>> From: Claudiu Zissulescu <claudiu.zissulescu-ianculescu@oracle.com>
>>
>> The SFrame is well supported by x86 architecture since binutils 2.41.
>> Enable it to be used as default frame tracer.
>>
>> Signed-off-by: Claudiu Zissulescu <claudiu.zissulescu-
>> ianculescu@oracle.com>
> 
>> diff --git a/sysdeps/unix/sysv/linux/x86/uw-sigframe.h b/sysdeps/unix/
>> sysv/linux/x86/uw-sigframe.h
> 
>> +#else /* ifdef __x86_64__  */
> 
> Maybe the following is less confusing:
> 
> #else /* !__x86_64__  */
> 
>> +
>> +#define MD_DECODE_SIGNAL_FRAME x86_decode_signal_frame
>> +
>> +static _Unwind_Reason_Code
>> +x86_fallback_frame_state (frame *frame)
> 
> I could not find any further reference to x86_fallback_frame_state.
> 

Ups :) my fault :)

Thank you Jens for your feedback! I'll address it on my second round,
Claudiu
  

Patch

diff --git a/sysdeps/unix/sysv/linux/x86/uw-sigframe.h b/sysdeps/unix/sysv/linux/x86/uw-sigframe.h
new file mode 100644
index 0000000000..daf2200c31
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/x86/uw-sigframe.h
@@ -0,0 +1,99 @@ 
+/* Signal frame backtracing support for SFrame on AMD, x86-64 and x86.
+   Copyright (C) 2025 Free Software Foundation, Inc.
+
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public License
+   as published by the Free Software Foundation; either version 2.1 of
+   the License, or (at your option) any later version.
+
+   The GNU C Library 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 Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+/* This code is inspired from libgcc's MD_FALLBACK_FRAME_STATE_FOR
+   implementation.  See libgcc/config/i386/gnu-unwind.h  */
+
+#include <signal.h>
+#include <sys/ucontext.h>
+
+#ifdef __x86_64__
+
+#define MD_DECODE_SIGNAL_FRAME x86_64_decode_signal_frame
+
+static _Unwind_Reason_Code
+x86_64_decode_signal_frame (frame *frame)
+{
+  unsigned char *pc = (unsigned char *) frame->pc;
+  struct sigcontext *sc;
+
+  /* movq $__NR_rt_sigreturn, %rax ; syscall.  */
+#ifdef __LP64__
+#define RT_SIGRETURN_SYSCALL	0x050f0000000fc0c7ULL
+#else
+#define RT_SIGRETURN_SYSCALL	0x050f40000201c0c7ULL
+#endif
+  if (*(unsigned char *)(pc+0) == 0x48
+      && *(unsigned long long *)(pc+1) == RT_SIGRETURN_SYSCALL)
+    {
+      ucontext_t *uc_ = (ucontext_t *)frame->sp;
+      /* The void * cast is necessary to avoid an aliasing warning.
+         The aliasing warning is correct, but should not be a problem
+         because it does not alias anything.  */
+      sc = (struct sigcontext *) (void *) &uc_->uc_mcontext;
+    }
+  else
+    return _URC_END_OF_STACK;
+
+  frame->pc = (_Unwind_Ptr) sc->rip;
+  frame->sp = (_Unwind_Ptr) sc->rsp;
+  frame->fp = (_Unwind_Ptr) sc->rbp;
+  return _URC_NO_REASON;
+}
+#else /* ifdef __x86_64__  */
+
+#define MD_DECODE_SIGNAL_FRAME x86_decode_signal_frame
+
+static _Unwind_Reason_Code
+x86_fallback_frame_state (frame *frame)
+{
+  unsigned char *pc = (unsigned char *) frame->pc;
+  struct sigcontext *sc;
+
+  /* popl %eax ; movl $__NR_sigreturn,%eax ; int $0x80  */
+  if (*(unsigned short *)(pc+0) == 0xb858
+      && *(unsigned int *)(pc+2) == 119
+      && *(unsigned short *)(pc+6) == 0x80cd)
+    sc = context->cfa + 4;
+  /* movl $__NR_rt_sigreturn,%eax ; int $0x80  */
+  else if (*(unsigned char *)(pc+0) == 0xb8
+	   && *(unsigned int *)(pc+1) == 173
+	   && *(unsigned short *)(pc+5) == 0x80cd)
+    {
+      struct rt_sigframe {
+	int sig;
+	siginfo_t *pinfo;
+	void *puc;
+	siginfo_t info;
+	ucontext_t uc;
+      } *rt_ = context->cfa;
+      /* The void * cast is necessary to avoid an aliasing warning.
+         The aliasing warning is correct, but should not be a problem
+         because it does not alias anything.  */
+      sc = (struct sigcontext *) (void *) &rt_->uc.uc_mcontext;
+    }
+  else
+    return _URC_END_OF_STACK;
+
+  frame->pc = (_Unwind_Ptr) sc->eip;
+  frame->sp = (_Unwind_Ptr) sc->esp;
+  frame->fp = (_Unwind_Ptr) sc->ebp;
+  return  _URC_NO_REASON;
+}
+#endif /* ifdef __x86_64__  */
diff --git a/sysdeps/x86/bits/dl_find_object.h b/sysdeps/x86/bits/dl_find_object.h
index 00860f4fbe..56f7b0cbda 100644
--- a/sysdeps/x86/bits/dl_find_object.h
+++ b/sysdeps/x86/bits/dl_find_object.h
@@ -27,3 +27,5 @@ 
 #endif
 #define DLFO_STRUCT_HAS_EH_COUNT 0
 #define DLFO_EH_SEGMENT_TYPE PT_GNU_EH_FRAME
+#define DLFO_SFRAME_SEGMENT_TYPE PT_GNU_SFRAME
+#define DLFO_STRUCT_HAS_SFRAME 1