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