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

01917d
https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=216711
01917d
01917d
FIXME: This workaround should be dropped and
01917d
glibc/sysdeps/unix/sysv/linux/x86_64/clone.S should get CFI for the child
01917d
instead.
01917d
01917d
2006-12-17  Jan Kratochvil  <jan.kratochvil@redhat.com>
01917d
01917d
	* gdb/amd64-linux-tdep.c (linux_clone_code): New variable.
01917d
	(LINUX_CLONE_LEN): New definition.
01917d
	(amd64_linux_clone_running, amd64_linux_outermost_frame): New function.
01917d
	(amd64_linux_init_abi): Initialize `outermost_frame_p'.
01917d
	* gdb/i386-tdep.c (i386_gdbarch_init): Likewise.
01917d
	* gdb/i386-tdep.h (gdbarch_tdep): Add `outermost_frame_p' member.
01917d
	* gdb/amd64-tdep.c (amd64_frame_this_id): Call `outermost_frame_p'.
01917d
01917d
2006-12-17  Jan Kratochvil  <jan.kratochvil@redhat.com>
01917d
01917d
	* gdb.threads/bt-clone-stop.exp, gdb.threads/bt-clone-stop.c:
01917d
	New file.
01917d
01917d
2007-10-16  Jan Kratochvil  <jan.kratochvil@redhat.com>
01917d
01917d
	Port to GDB-6.7.
01917d
01917d
Index: gdb-7.4.50.20120703/gdb/amd64-linux-tdep.c
01917d
===================================================================
01917d
--- gdb-7.4.50.20120703.orig/gdb/amd64-linux-tdep.c	2012-06-13 22:36:48.000000000 +0200
01917d
+++ gdb-7.4.50.20120703/gdb/amd64-linux-tdep.c	2012-07-03 17:32:46.547563363 +0200
01917d
@@ -271,6 +271,80 @@ amd64_linux_register_reggroup_p (struct
01917d
 
01917d
 /* Set the program counter for process PTID to PC.  */
01917d
 
01917d
+/* Detect the outermost frame; during unwind of
01917d
+   	#5  0x000000305cec68c3 in clone () from /lib64/tls/libc.so.6
01917d
+   avoid the additional bogus frame
01917d
+   	#6  0x0000000000000000 in ??
01917d
+   We compare if the `linux_clone_code' block is _before_ unwound PC.  */
01917d
+
01917d
+static const unsigned char linux_clone_code[] =
01917d
+{
01917d
+/* libc/sysdeps/unix/sysv/linux/x86_64/clone.S */
01917d
+/* #ifdef RESET_PID */
01917d
+/* ... */
01917d
+/* 	mov	$SYS_ify(getpid), %eax */
01917d
+/* 0xb8, 0x27, 0x00, 0x00, 0x00 */
01917d
+/* OR */
01917d
+/* 	mov	$SYS_ify(getpid), %rax */
01917d
+/* 0x48, 0xc7, 0xc0, 0x27, 0x00, 0x00, 0x00 */
01917d
+/* so just: */
01917d
+  0x27, 0x00, 0x00, 0x00,
01917d
+/* 	syscall */
01917d
+  0x0f, 0x05,
01917d
+/* 	movl	%eax, %fs:PID */
01917d
+  0x64, 0x89, 0x04, 0x25, 0x94, 0x00, 0x00, 0x00,
01917d
+/* 	movl	%eax, %fs:TID */
01917d
+  0x64, 0x89, 0x04, 0x25, 0x90, 0x00, 0x00, 0x00,
01917d
+/* #endif */
01917d
+/* 	|* Set up arguments for the function call.  *| */
01917d
+/* 	popq	%rax		|* Function to call.  *| */
01917d
+  0x58,
01917d
+/* 	popq	%rdi		|* Argument.  *| */
01917d
+  0x5f,
01917d
+/* 	call	*%rax$   */
01917d
+  0xff, 0xd0
01917d
+};
01917d
+
01917d
+#define LINUX_CLONE_LEN (sizeof linux_clone_code)
01917d
+
01917d
+static int
01917d
+amd64_linux_clone_running (struct frame_info *this_frame)
01917d
+{
01917d
+  CORE_ADDR pc = get_frame_pc (this_frame);
01917d
+  unsigned char buf[LINUX_CLONE_LEN];
01917d
+
01917d
+  if (!safe_frame_unwind_memory (this_frame, pc - LINUX_CLONE_LEN, buf,
01917d
+				 LINUX_CLONE_LEN))
01917d
+    return 0;
01917d
+
01917d
+  if (memcmp (buf, linux_clone_code, LINUX_CLONE_LEN) != 0)
01917d
+    return 0;
01917d
+
01917d
+  return 1;
01917d
+}
01917d
+
01917d
+static int
01917d
+amd64_linux_outermost_frame (struct frame_info *this_frame)
01917d
+{
01917d
+  CORE_ADDR pc = get_frame_pc (this_frame);
01917d
+  const char *name;
01917d
+
01917d
+  find_pc_partial_function (pc, &name, NULL, NULL);
01917d
+
01917d
+  /* If we have NAME, we can optimize the search.
01917d
+     `clone' NAME still needs to have the code checked as its name may be
01917d
+     present in the user code.
01917d
+     `__clone' NAME should not be present in the user code but in the initial
01917d
+     parts of the `__clone' implementation the unwind still makes sense.
01917d
+     More detailed unwinding decision would be too much sensitive to possible
01917d
+     subtle changes in specific glibc revisions.  */
01917d
+  if (name == NULL || strcmp (name, "clone") == 0
01917d
+      || strcmp ("__clone", name) == 0)
01917d
+    return (amd64_linux_clone_running (this_frame) != 0);
01917d
+
01917d
+  return 0;
01917d
+}
01917d
+
01917d
 static void
01917d
 amd64_linux_write_pc (struct regcache *regcache, CORE_ADDR pc)
01917d
 {
01917d
@@ -1547,6 +1621,8 @@ amd64_linux_init_abi (struct gdbarch_inf
01917d
 
01917d
   amd64_linux_init_abi_common (info, gdbarch);
01917d
 
01917d
+  tdep->outermost_frame_p = amd64_linux_outermost_frame;
01917d
+
01917d
   /* GNU/Linux uses SVR4-style shared libraries.  */
01917d
   set_solib_svr4_fetch_link_map_offsets
01917d
     (gdbarch, svr4_lp64_fetch_link_map_offsets);
01917d
Index: gdb-7.4.50.20120703/gdb/amd64-tdep.c
01917d
===================================================================
01917d
--- gdb-7.4.50.20120703.orig/gdb/amd64-tdep.c	2012-06-16 17:20:22.000000000 +0200
01917d
+++ gdb-7.4.50.20120703/gdb/amd64-tdep.c	2012-07-03 17:32:12.335604415 +0200
01917d
@@ -2324,6 +2324,7 @@ amd64_frame_unwind_stop_reason (struct f
01917d
 {
01917d
   struct amd64_frame_cache *cache =
01917d
     amd64_frame_cache (this_frame, this_cache);
01917d
+  struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (this_frame));
01917d
 
01917d
   if (!cache->base_p)
01917d
     return UNWIND_UNAVAILABLE;
01917d
@@ -2332,6 +2333,10 @@ amd64_frame_unwind_stop_reason (struct f
01917d
   if (cache->base == 0)
01917d
     return UNWIND_OUTERMOST;
01917d
 
01917d
+  /* Detect OS dependent outermost frames; such as `clone'.  */
01917d
+  if (tdep->outermost_frame_p && tdep->outermost_frame_p (this_frame))
01917d
+    return UNWIND_OUTERMOST;
01917d
+
01917d
   return UNWIND_NO_REASON;
01917d
 }
01917d
 
01917d
@@ -2341,6 +2346,7 @@ amd64_frame_this_id (struct frame_info *
01917d
 {
01917d
   struct amd64_frame_cache *cache =
01917d
     amd64_frame_cache (this_frame, this_cache);
01917d
+  struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (this_frame));
01917d
 
01917d
   if (!cache->base_p)
01917d
     return;
01917d
@@ -2349,6 +2355,10 @@ amd64_frame_this_id (struct frame_info *
01917d
   if (cache->base == 0)
01917d
     return;
01917d
 
01917d
+  /* Detect OS dependent outermost frames; such as `clone'.  */
01917d
+  if (tdep->outermost_frame_p && tdep->outermost_frame_p (this_frame))
01917d
+    return;
01917d
+
01917d
   (*this_id) = frame_id_build (cache->base + 16, cache->pc);
01917d
 }
01917d
 
01917d
Index: gdb-7.4.50.20120703/gdb/i386-tdep.c
01917d
===================================================================
01917d
--- gdb-7.4.50.20120703.orig/gdb/i386-tdep.c	2012-06-18 19:31:34.000000000 +0200
01917d
+++ gdb-7.4.50.20120703/gdb/i386-tdep.c	2012-07-03 17:32:12.339604409 +0200
01917d
@@ -7655,6 +7655,9 @@ i386_gdbarch_init (struct gdbarch_info i
01917d
 
01917d
   tdep->xsave_xcr0_offset = -1;
01917d
 
01917d
+  /* Unwinding stops on i386 automatically.  */
01917d
+  tdep->outermost_frame_p = NULL;
01917d
+
01917d
   tdep->record_regmap = i386_record_regmap;
01917d
 
01917d
   set_gdbarch_long_long_align_bit (gdbarch, 32);
01917d
Index: gdb-7.4.50.20120703/gdb/i386-tdep.h
01917d
===================================================================
01917d
--- gdb-7.4.50.20120703.orig/gdb/i386-tdep.h	2012-06-13 22:29:15.000000000 +0200
01917d
+++ gdb-7.4.50.20120703/gdb/i386-tdep.h	2012-07-03 17:32:12.340604408 +0200
01917d
@@ -219,6 +219,9 @@ struct gdbarch_tdep
01917d
   int (*i386_sysenter_record) (struct regcache *regcache);
01917d
   /* Parse syscall args.  */
01917d
   int (*i386_syscall_record) (struct regcache *regcache);
01917d
+
01917d
+  /* Detect OS dependent outermost frames; such as `clone'.  */
01917d
+  int (*outermost_frame_p) (struct frame_info *this_frame);
01917d
 };
01917d
 
01917d
 /* Floating-point registers.  */
01917d
Index: gdb-7.4.50.20120703/gdb/ia64-tdep.c
01917d
===================================================================
01917d
--- gdb-7.4.50.20120703.orig/gdb/ia64-tdep.c	2012-07-03 17:30:09.000000000 +0200
01917d
+++ gdb-7.4.50.20120703/gdb/ia64-tdep.c	2012-07-03 17:32:12.343604405 +0200
01917d
@@ -2176,6 +2176,138 @@ static const struct frame_unwind ia64_fr
01917d
   default_frame_sniffer
01917d
 };
01917d
 
01917d
+/* Detect the outermost frame; during unwind of
01917d
+   	#6  0x2000000000347100 in __clone2 () from /lib/libc.so.6.1
01917d
+   avoid the additional bogus frame
01917d
+   	#7  0x0000000000000000 in ?? ()  */
01917d
+
01917d
+static char linux_clone2_code[] =
01917d
+{
01917d
+/* libc/sysdeps/unix/sysv/linux/ia64/clone2.S */
01917d
+  0x09, 0x00, 0x20, 0x12, 0x90, 0x11, 0x00, 0x40,
01917d
+  0x28, 0x20, 0x23, 0x00, 0x00, 0x00, 0x04, 0x00,
01917d
+/*         st4 [r9]=r8 */
01917d
+/*         st4 [r10]=r8 */
01917d
+/*         ;; */
01917d
+/* #endif */
01917d
+  0x02, 0x50, 0x21, 0x40, 0x18, 0x14, 0x90, 0x02,
01917d
+  0x90, 0x00, 0x42, 0x00, 0x00, 0x00, 0x04, 0x00,
01917d
+/* 1:      ld8 out1=[in0],8        |* Retrieve code pointer.       *| */
01917d
+/*         mov out0=in4            |* Pass proper argument to fn *| */
01917d
+/*         ;; */
01917d
+  0x11, 0x08, 0x00, 0x40, 0x18, 0x10, 0x60, 0x50,
01917d
+  0x05, 0x80, 0x03, 0x00, 0x68, 0x00, 0x80, 0x12,
01917d
+/*         ld8 gp=[in0]            |* Load function gp.            *| */
01917d
+/*         mov b6=out1 */
01917d
+/*         br.call.dptk.many rp=b6 |* Call fn(arg) in the child    *| */
01917d
+/*         ;; */
01917d
+  0x10, 0x48, 0x01, 0x10, 0x00, 0x21, 0x10, 0x00,
01917d
+  0xa0, 0x00, 0x42, 0x00, 0x98, 0xdf, 0xf7, 0x5b,
01917d
+/*         mov out0=r8             |* Argument to _exit            *| */
01917d
+/*         mov gp=loc0 */
01917d
+/*         .globl HIDDEN_JUMPTARGET(_exit) */
01917d
+/*         br.call.dpnt.many rp=HIDDEN_JUMPTARGET(_exit) */
01917d
+/*                                 |* call _exit with result from fn.      *| */
01917d
+  0x11, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
01917d
+  0x00, 0x02, 0x00, 0x80, 0x00, 0x00, 0x84, 0x00
01917d
+/*         ret                     |* Not reached.         *| */
01917d
+};
01917d
+
01917d
+#define LINUX_CLONE_PRE_SLOTS 3	/* Number of slots before PC.  */
01917d
+#define LINUX_CLONE_LEN (sizeof linux_clone2_code)
01917d
+
01917d
+static int
01917d
+ia64_linux_clone2_running (struct frame_info *this_frame)
01917d
+{
01917d
+  CORE_ADDR pc = get_frame_pc (this_frame);
01917d
+  char buf[LINUX_CLONE_LEN];
01917d
+  struct minimal_symbol *minsym;
01917d
+  long long instr;
01917d
+
01917d
+  if (!safe_frame_unwind_memory (this_frame, pc - LINUX_CLONE_PRE_SLOTS * 16,
01917d
+				 buf, LINUX_CLONE_LEN))
01917d
+    return 0;
01917d
+
01917d
+  if (memcmp (buf, linux_clone2_code, LINUX_CLONE_PRE_SLOTS * 16) != 0)
01917d
+    return 0;
01917d
+
01917d
+  /* Adjust the expected "_exit" address.  */
01917d
+  minsym = lookup_minimal_symbol_text ("_exit", NULL);
01917d
+  if (minsym == NULL)
01917d
+    return 0;
01917d
+
01917d
+  instr = slotN_contents (&linux_clone2_code[LINUX_CLONE_PRE_SLOTS * 16], 2);
01917d
+  instr &= ~(((1L << 20) - 1) << 13);
01917d
+  /* Address is relative to the jump instruction slot, not the next one.  */
01917d
+  instr |= (((SYMBOL_VALUE_ADDRESS (minsym) - (pc & ~0xfL)) >> 4)
01917d
+	    & ((1L << 20) - 1)) << 13;
01917d
+  replace_slotN_contents (&linux_clone2_code[LINUX_CLONE_PRE_SLOTS * 16], instr,
01917d
+			  2);
01917d
+
01917d
+  if (memcmp (&buf[LINUX_CLONE_PRE_SLOTS * 16],
01917d
+              &linux_clone2_code[LINUX_CLONE_PRE_SLOTS * 16],
01917d
+	      LINUX_CLONE_LEN - (LINUX_CLONE_PRE_SLOTS * 16)) != 0)
01917d
+    return 0;
01917d
+
01917d
+  return 1;
01917d
+}
01917d
+
01917d
+static int
01917d
+ia64_outermost_frame (struct frame_info *this_frame)
01917d
+{
01917d
+  CORE_ADDR pc = get_frame_pc (this_frame);
01917d
+  char *name;
01917d
+
01917d
+  find_pc_partial_function (pc, &name, NULL, NULL);
01917d
+
01917d
+  /* If we have NAME, we can optimize the search.
01917d
+     `clone' NAME still needs to have the code checked as its name may be
01917d
+     present in the user code.
01917d
+     `__clone' NAME should not be present in the user code but in the initial
01917d
+     parts of the `__clone' implementation the unwind still makes sense.
01917d
+     More detailed unwinding decision would be too much sensitive to possible
01917d
+     subtle changes in specific glibc revisions.  */
01917d
+  if (name == NULL || strcmp (name, "clone2") == 0
01917d
+      || strcmp ("__clone2", name) == 0)
01917d
+    return (ia64_linux_clone2_running (this_frame) != 0);
01917d
+
01917d
+  return 0;
01917d
+}
01917d
+
01917d
+static void
01917d
+ia64_clone2_frame_this_id (struct frame_info *this_frame, void **this_cache,
01917d
+			   struct frame_id *this_id)
01917d
+{
01917d
+  /* Leave the default outermost frame at *THIS_ID.  */
01917d
+}
01917d
+
01917d
+static struct value *
01917d
+ia64_clone2_frame_prev_register (struct frame_info *this_frame,
01917d
+				 void **this_cache, int regnum)
01917d
+{
01917d
+  return frame_unwind_got_register (this_frame, regnum, regnum);
01917d
+}
01917d
+
01917d
+static int
01917d
+ia64_clone2_frame_sniffer (const struct frame_unwind *self,
01917d
+			   struct frame_info *this_frame,
01917d
+			   void **this_prologue_cache)
01917d
+{
01917d
+  if (ia64_outermost_frame (this_frame))
01917d
+    return 1;
01917d
+
01917d
+  return 0;
01917d
+}
01917d
+
01917d
+static const struct frame_unwind ia64_clone2_frame_unwind =
01917d
+{
01917d
+  NORMAL_FRAME,
01917d
+  &ia64_clone2_frame_this_id,
01917d
+  &ia64_clone2_frame_prev_register,
01917d
+  NULL,
01917d
+  &ia64_clone2_frame_sniffer
01917d
+};
01917d
+
01917d
 /* Signal trampolines.  */
01917d
 
01917d
 static void
01917d
@@ -4146,6 +4278,7 @@ ia64_gdbarch_init (struct gdbarch_info i
01917d
   set_gdbarch_dummy_id (gdbarch, ia64_dummy_id);
01917d
 
01917d
   set_gdbarch_unwind_pc (gdbarch, ia64_unwind_pc);
01917d
+  frame_unwind_append_unwinder (gdbarch, &ia64_clone2_frame_unwind);
01917d
 #ifdef HAVE_LIBUNWIND_IA64_H
01917d
   frame_unwind_append_unwinder (gdbarch,
01917d
                                 &ia64_libunwind_sigtramp_frame_unwind);
01917d
Index: gdb-7.4.50.20120703/gdb/testsuite/gdb.threads/bt-clone-stop.c
01917d
===================================================================
01917d
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
01917d
+++ gdb-7.4.50.20120703/gdb/testsuite/gdb.threads/bt-clone-stop.c	2012-07-03 17:32:12.344604404 +0200
01917d
@@ -0,0 +1,39 @@
01917d
+/* This testcase is part of GDB, the GNU debugger.
01917d
+
01917d
+   Copyright 2006 Free Software Foundation, Inc.
01917d
+
01917d
+   This program is free software; you can redistribute it and/or modify
01917d
+   it under the terms of the GNU General Public License as published by
01917d
+   the Free Software Foundation; either version 2 of the License, or
01917d
+   (at your option) any later version.
01917d
+
01917d
+   This program is distributed in the hope that it will be useful,
01917d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
01917d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
01917d
+   GNU General Public License for more details.
01917d
+ 
01917d
+   You should have received a copy of the GNU General Public License
01917d
+   along with this program; if not, write to the Free Software
01917d
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
01917d
+   MA 02110-1301, USA.  */
01917d
+
01917d
+
01917d
+#include <pthread.h>
01917d
+#include <unistd.h>
01917d
+#include <assert.h>
01917d
+
01917d
+
01917d
+void *threader (void *arg)
01917d
+{
01917d
+	assert (0);
01917d
+	return NULL;
01917d
+}
01917d
+
01917d
+int main (void)
01917d
+{
01917d
+	pthread_t t1;
01917d
+
01917d
+	pthread_create (&t1, NULL, threader, (void *) NULL);
01917d
+	for (;;)
01917d
+		pause();
01917d
+}
01917d
Index: gdb-7.4.50.20120703/gdb/testsuite/gdb.threads/bt-clone-stop.exp
01917d
===================================================================
01917d
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
01917d
+++ gdb-7.4.50.20120703/gdb/testsuite/gdb.threads/bt-clone-stop.exp	2012-07-03 17:32:12.344604404 +0200
01917d
@@ -0,0 +1,61 @@
01917d
+# Copyright 2006 Free Software Foundation, Inc.
01917d
+
01917d
+# This program is free software; you can redistribute it and/or modify
01917d
+# it under the terms of the GNU General Public License as published by
01917d
+# the Free Software Foundation; either version 2 of the License, or
01917d
+# (at your option) any later version.
01917d
+# 
01917d
+# This program is distributed in the hope that it will be useful,
01917d
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
01917d
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
01917d
+# GNU General Public License for more details.
01917d
+# 
01917d
+# You should have received a copy of the GNU General Public License
01917d
+# along with this program; if not, write to the Free Software
01917d
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  
01917d
+
01917d
+# Backtraced `clone' must not have `PC == 0' as its previous frame.
01917d
+
01917d
+if $tracelevel then {
01917d
+    strace $tracelevel
01917d
+}
01917d
+
01917d
+set testfile bt-clone-stop
01917d
+set srcfile ${testfile}.c
01917d
+set binfile ${objdir}/${subdir}/${testfile}
01917d
+if  { [gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
01917d
+    untested "Couldn't compile test program"
01917d
+    return -1
01917d
+}
01917d
+
01917d
+# Get things started.
01917d
+
01917d
+gdb_exit
01917d
+gdb_start
01917d
+gdb_reinitialize_dir $srcdir/$subdir
01917d
+gdb_load ${binfile}
01917d
+
01917d
+# threader: threader.c:8: threader: Assertion `0' failed.
01917d
+# Program received signal SIGABRT, Aborted.
01917d
+
01917d
+gdb_test "run" \
01917d
+     "Program received signal SIGABRT.*" \
01917d
+     "run"
01917d
+
01917d
+# Former gdb unwind (the first function is `clone'):
01917d
+# #5  0x0000003421ecd62d in ?? () from /lib64/libc.so.6
01917d
+# #6  0x0000000000000000 in ?? ()
01917d
+# (gdb)
01917d
+# Tested `amd64_linux_outermost_frame' functionality should omit the line `#6'.
01917d
+# 
01917d
+# Two `-re' cases below must be in this order (1st is a subset of the 2nd one).
01917d
+# Unhandled case below should not happen and it is fortunately handled by
01917d
+# `amd64_linux_outermost_frame' as FAIL (and result `0x0 entry output invalid').
01917d
+gdb_test_multiple "bt" "0x0 entry output invalid" {
01917d
+    -re "in threader \\(.*\n#\[0-9\]* *0x0* in .*$gdb_prompt $" {
01917d
+    	fail "0x0 entry found"
01917d
+    }
01917d
+    -re "in threader \\(.*$gdb_prompt $" {
01917d
+    	pass "0x0 entry not found"
01917d
+    }
01917d
+}