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

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