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

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