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

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