Blame SOURCES/gdb-6.5-bz216711-clone-is-outermost.patch

6240d7
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
6240d7
From: Jan Kratochvil <jan.kratochvil@redhat.com>
6240d7
Date: Fri, 27 Oct 2017 21:07:50 +0200
6240d7
Subject: gdb-6.5-bz216711-clone-is-outermost.patch
6240d7
6240d7
;; Fix bogus 0x0 unwind of the thread's topmost function clone(3) (BZ 216711).
6240d7
;;=fedora
6240d7
6240d7
https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=216711
6240d7
6240d7
FIXME: This workaround should be dropped and
6240d7
glibc/sysdeps/unix/sysv/linux/x86_64/clone.S should get CFI for the child
6240d7
instead.
6240d7
6240d7
2006-12-17  Jan Kratochvil  <jan.kratochvil@redhat.com>
6240d7
6240d7
	* gdb/amd64-linux-tdep.c (linux_clone_code): New variable.
6240d7
	(LINUX_CLONE_LEN): New definition.
6240d7
	(amd64_linux_clone_running, amd64_linux_outermost_frame): New function.
6240d7
	(amd64_linux_init_abi): Initialize `outermost_frame_p'.
6240d7
	* gdb/i386-tdep.c (i386_gdbarch_init): Likewise.
6240d7
	* gdb/i386-tdep.h (gdbarch_tdep): Add `outermost_frame_p' member.
6240d7
	* gdb/amd64-tdep.c (amd64_frame_this_id): Call `outermost_frame_p'.
6240d7
6240d7
2006-12-17  Jan Kratochvil  <jan.kratochvil@redhat.com>
6240d7
6240d7
	* gdb.threads/bt-clone-stop.exp, gdb.threads/bt-clone-stop.c:
6240d7
	New file.
6240d7
6240d7
2007-10-16  Jan Kratochvil  <jan.kratochvil@redhat.com>
6240d7
6240d7
	Port to GDB-6.7.
6240d7
6240d7
diff --git a/gdb/amd64-linux-tdep.c b/gdb/amd64-linux-tdep.c
6240d7
--- a/gdb/amd64-linux-tdep.c
6240d7
+++ b/gdb/amd64-linux-tdep.c
6240d7
@@ -291,6 +291,80 @@ amd64_linux_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
6240d7
 
6240d7
 /* Set the program counter for process PTID to PC.  */
6240d7
 
6240d7
+/* Detect the outermost frame; during unwind of
6240d7
+   	#5  0x000000305cec68c3 in clone () from /lib64/tls/libc.so.6
6240d7
+   avoid the additional bogus frame
6240d7
+   	#6  0x0000000000000000 in ??
6240d7
+   We compare if the `linux_clone_code' block is _before_ unwound PC.  */
6240d7
+
6240d7
+static const unsigned char linux_clone_code[] =
6240d7
+{
6240d7
+/* libc/sysdeps/unix/sysv/linux/x86_64/clone.S */
6240d7
+/* #ifdef RESET_PID */
6240d7
+/* ... */
6240d7
+/* 	mov	$SYS_ify(getpid), %eax */
6240d7
+/* 0xb8, 0x27, 0x00, 0x00, 0x00 */
6240d7
+/* OR */
6240d7
+/* 	mov	$SYS_ify(getpid), %rax */
6240d7
+/* 0x48, 0xc7, 0xc0, 0x27, 0x00, 0x00, 0x00 */
6240d7
+/* so just: */
6240d7
+  0x27, 0x00, 0x00, 0x00,
6240d7
+/* 	syscall */
6240d7
+  0x0f, 0x05,
6240d7
+/* 	movl	%eax, %fs:PID */
6240d7
+  0x64, 0x89, 0x04, 0x25, 0x94, 0x00, 0x00, 0x00,
6240d7
+/* 	movl	%eax, %fs:TID */
6240d7
+  0x64, 0x89, 0x04, 0x25, 0x90, 0x00, 0x00, 0x00,
6240d7
+/* #endif */
6240d7
+/* 	|* Set up arguments for the function call.  *| */
6240d7
+/* 	popq	%rax		|* Function to call.  *| */
6240d7
+  0x58,
6240d7
+/* 	popq	%rdi		|* Argument.  *| */
6240d7
+  0x5f,
6240d7
+/* 	call	*%rax$   */
6240d7
+  0xff, 0xd0
6240d7
+};
6240d7
+
6240d7
+#define LINUX_CLONE_LEN (sizeof linux_clone_code)
6240d7
+
6240d7
+static int
6240d7
+amd64_linux_clone_running (struct frame_info *this_frame)
6240d7
+{
6240d7
+  CORE_ADDR pc = get_frame_pc (this_frame);
6240d7
+  unsigned char buf[LINUX_CLONE_LEN];
6240d7
+
6240d7
+  if (!safe_frame_unwind_memory (this_frame, pc - LINUX_CLONE_LEN, buf,
6240d7
+				 LINUX_CLONE_LEN))
6240d7
+    return 0;
6240d7
+
6240d7
+  if (memcmp (buf, linux_clone_code, LINUX_CLONE_LEN) != 0)
6240d7
+    return 0;
6240d7
+
6240d7
+  return 1;
6240d7
+}
6240d7
+
6240d7
+static int
6240d7
+amd64_linux_outermost_frame (struct frame_info *this_frame)
6240d7
+{
6240d7
+  CORE_ADDR pc = get_frame_pc (this_frame);
6240d7
+  const char *name;
6240d7
+
6240d7
+  find_pc_partial_function (pc, &name, NULL, NULL);
6240d7
+
6240d7
+  /* If we have NAME, we can optimize the search.
6240d7
+     `clone' NAME still needs to have the code checked as its name may be
6240d7
+     present in the user code.
6240d7
+     `__clone' NAME should not be present in the user code but in the initial
6240d7
+     parts of the `__clone' implementation the unwind still makes sense.
6240d7
+     More detailed unwinding decision would be too much sensitive to possible
6240d7
+     subtle changes in specific glibc revisions.  */
6240d7
+  if (name == NULL || strcmp (name, "clone") == 0
6240d7
+      || strcmp ("__clone", name) == 0)
6240d7
+    return (amd64_linux_clone_running (this_frame) != 0);
6240d7
+
6240d7
+  return 0;
6240d7
+}
6240d7
+
6240d7
 static void
6240d7
 amd64_linux_write_pc (struct regcache *regcache, CORE_ADDR pc)
6240d7
 {
6240d7
@@ -1808,6 +1882,8 @@ amd64_linux_init_abi_common(struct gdbarch_info info, struct gdbarch *gdbarch)
6240d7
 
6240d7
   tdep->xsave_xcr0_offset = I386_LINUX_XSAVE_XCR0_OFFSET;
6240d7
 
6240d7
+  tdep->outermost_frame_p = amd64_linux_outermost_frame;
6240d7
+
6240d7
   /* Add the %orig_rax register used for syscall restarting.  */
6240d7
   set_gdbarch_write_pc (gdbarch, amd64_linux_write_pc);
6240d7
 
6240d7
diff --git a/gdb/amd64-tdep.c b/gdb/amd64-tdep.c
6240d7
--- a/gdb/amd64-tdep.c
6240d7
+++ b/gdb/amd64-tdep.c
6240d7
@@ -2601,6 +2601,7 @@ amd64_frame_unwind_stop_reason (struct frame_info *this_frame,
6240d7
 {
6240d7
   struct amd64_frame_cache *cache =
6240d7
     amd64_frame_cache (this_frame, this_cache);
6240d7
+  struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (this_frame));
6240d7
 
6240d7
   if (!cache->base_p)
6240d7
     return UNWIND_UNAVAILABLE;
6240d7
@@ -2609,6 +2610,10 @@ amd64_frame_unwind_stop_reason (struct frame_info *this_frame,
6240d7
   if (cache->base == 0)
6240d7
     return UNWIND_OUTERMOST;
6240d7
 
6240d7
+  /* Detect OS dependent outermost frames; such as `clone'.  */
6240d7
+  if (tdep->outermost_frame_p && tdep->outermost_frame_p (this_frame))
6240d7
+    return UNWIND_OUTERMOST;
6240d7
+
6240d7
   return UNWIND_NO_REASON;
6240d7
 }
6240d7
 
6240d7
@@ -2743,6 +2748,7 @@ amd64_sigtramp_frame_this_id (struct frame_info *this_frame,
6240d7
 {
6240d7
   struct amd64_frame_cache *cache =
6240d7
     amd64_sigtramp_frame_cache (this_frame, this_cache);
6240d7
+  struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (this_frame));
6240d7
 
6240d7
   if (!cache->base_p)
6240d7
     (*this_id) = frame_id_build_unavailable_stack (get_frame_pc (this_frame));
6240d7
@@ -2751,6 +2757,11 @@ amd64_sigtramp_frame_this_id (struct frame_info *this_frame,
6240d7
       /* This marks the outermost frame.  */
6240d7
       return;
6240d7
     }
6240d7
+  else if (tdep->outermost_frame_p && tdep->outermost_frame_p (this_frame))
6240d7
+    {
6240d7
+      /* Detect OS dependent outermost frames; such as `clone'.  */
6240d7
+      return;
6240d7
+    }
6240d7
   else
6240d7
     (*this_id) = frame_id_build (cache->base + 16, get_frame_pc (this_frame));
6240d7
 }
6240d7
diff --git a/gdb/i386-tdep.c b/gdb/i386-tdep.c
6240d7
--- a/gdb/i386-tdep.c
6240d7
+++ b/gdb/i386-tdep.c
6240d7
@@ -8407,6 +8407,9 @@ i386_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
6240d7
 
6240d7
   tdep->xsave_xcr0_offset = -1;
6240d7
 
6240d7
+  /* Unwinding stops on i386 automatically.  */
6240d7
+  tdep->outermost_frame_p = NULL;
6240d7
+
6240d7
   tdep->record_regmap = i386_record_regmap;
6240d7
 
6240d7
   set_gdbarch_type_align (gdbarch, i386_type_align);
6240d7
diff --git a/gdb/i386-tdep.h b/gdb/i386-tdep.h
6240d7
--- a/gdb/i386-tdep.h
6240d7
+++ b/gdb/i386-tdep.h
6240d7
@@ -251,6 +251,9 @@ struct gdbarch_tdep
6240d7
 
6240d7
   /* Regsets. */
6240d7
   const struct regset *fpregset;
6240d7
+
6240d7
+  /* Detect OS dependent outermost frames; such as `clone'.  */
6240d7
+  int (*outermost_frame_p) (struct frame_info *this_frame);
6240d7
 };
6240d7
 
6240d7
 /* Floating-point registers.  */
6240d7
diff --git a/gdb/testsuite/gdb.threads/bt-clone-stop.c b/gdb/testsuite/gdb.threads/bt-clone-stop.c
6240d7
new file mode 100644
6240d7
--- /dev/null
6240d7
+++ b/gdb/testsuite/gdb.threads/bt-clone-stop.c
6240d7
@@ -0,0 +1,39 @@
6240d7
+/* This testcase is part of GDB, the GNU debugger.
6240d7
+
6240d7
+   Copyright 2006 Free Software Foundation, Inc.
6240d7
+
6240d7
+   This program is free software; you can redistribute it and/or modify
6240d7
+   it under the terms of the GNU General Public License as published by
6240d7
+   the Free Software Foundation; either version 2 of the License, or
6240d7
+   (at your option) any later version.
6240d7
+
6240d7
+   This program is distributed in the hope that it will be useful,
6240d7
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
6240d7
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6240d7
+   GNU General Public License for more details.
6240d7
+ 
6240d7
+   You should have received a copy of the GNU General Public License
6240d7
+   along with this program; if not, write to the Free Software
6240d7
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
6240d7
+   MA 02110-1301, USA.  */
6240d7
+
6240d7
+
6240d7
+#include <pthread.h>
6240d7
+#include <unistd.h>
6240d7
+#include <assert.h>
6240d7
+
6240d7
+
6240d7
+void *threader (void *arg)
6240d7
+{
6240d7
+	assert (0);
6240d7
+	return NULL;
6240d7
+}
6240d7
+
6240d7
+int main (void)
6240d7
+{
6240d7
+	pthread_t t1;
6240d7
+
6240d7
+	pthread_create (&t1, NULL, threader, (void *) NULL);
6240d7
+	for (;;)
6240d7
+		pause();
6240d7
+}
6240d7
diff --git a/gdb/testsuite/gdb.threads/bt-clone-stop.exp b/gdb/testsuite/gdb.threads/bt-clone-stop.exp
6240d7
new file mode 100644
6240d7
--- /dev/null
6240d7
+++ b/gdb/testsuite/gdb.threads/bt-clone-stop.exp
6240d7
@@ -0,0 +1,61 @@
6240d7
+# Copyright 2006 Free Software Foundation, Inc.
6240d7
+
6240d7
+# This program is free software; you can redistribute it and/or modify
6240d7
+# it under the terms of the GNU General Public License as published by
6240d7
+# the Free Software Foundation; either version 2 of the License, or
6240d7
+# (at your option) any later version.
6240d7
+# 
6240d7
+# This program is distributed in the hope that it will be useful,
6240d7
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
6240d7
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6240d7
+# GNU General Public License for more details.
6240d7
+# 
6240d7
+# You should have received a copy of the GNU General Public License
6240d7
+# along with this program; if not, write to the Free Software
6240d7
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  
6240d7
+
6240d7
+# Backtraced `clone' must not have `PC == 0' as its previous frame.
6240d7
+
6240d7
+if $tracelevel then {
6240d7
+    strace $tracelevel
6240d7
+}
6240d7
+
6240d7
+set testfile bt-clone-stop
6240d7
+set srcfile ${testfile}.c
6240d7
+set binfile [standard_output_file ${testfile}]
6240d7
+if  { [gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
6240d7
+    untested "Couldn't compile test program"
6240d7
+    return -1
6240d7
+}
6240d7
+
6240d7
+# Get things started.
6240d7
+
6240d7
+gdb_exit
6240d7
+gdb_start
6240d7
+gdb_reinitialize_dir $srcdir/$subdir
6240d7
+gdb_load ${binfile}
6240d7
+
6240d7
+# threader: threader.c:8: threader: Assertion `0' failed.
6240d7
+# Program received signal SIGABRT, Aborted.
6240d7
+
6240d7
+gdb_test "run" \
6240d7
+     {Thread 2 "bt-clone-stop" received signal SIGABRT.*} \
6240d7
+     "run"
6240d7
+
6240d7
+# Former gdb unwind (the first function is `clone'):
6240d7
+# #5  0x0000003421ecd62d in ?? () from /lib64/libc.so.6
6240d7
+# #6  0x0000000000000000 in ?? ()
6240d7
+# (gdb)
6240d7
+# Tested `amd64_linux_outermost_frame' functionality should omit the line `#6'.
6240d7
+# 
6240d7
+# Two `-re' cases below must be in this order (1st is a subset of the 2nd one).
6240d7
+# Unhandled case below should not happen and it is fortunately handled by
6240d7
+# `amd64_linux_outermost_frame' as FAIL (and result `0x0 entry output invalid').
6240d7
+gdb_test_multiple "bt" "0x0 entry output invalid" {
6240d7
+    -re "in threader \\(.*\n#\[0-9\]* *0x0* in .*$gdb_prompt $" {
6240d7
+    	fail "0x0 entry found"
6240d7
+    }
6240d7
+    -re "in threader \\(.*$gdb_prompt $" {
6240d7
+    	pass "0x0 entry not found"
6240d7
+    }
6240d7
+}