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

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