keiths / rpms / gdb

Forked from rpms/gdb 12 days ago
Clone
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
e5611bf
Index: gdb-6.8.50.20090802/gdb/amd64-linux-tdep.c
eb9d945
===================================================================
e5611bf
--- gdb-6.8.50.20090802.orig/gdb/amd64-linux-tdep.c	2009-07-02 19:25:52.000000000 +0200
e5611bf
+++ gdb-6.8.50.20090802/gdb/amd64-linux-tdep.c	2009-08-03 15:50:08.000000000 +0200
e5611bf
@@ -238,6 +238,80 @@ amd64_linux_register_reggroup_p (struct 
b961095
 
b961095
 /* Set the program counter for process PTID to PC.  */
b961095
 
b961095
+/* Detect the outermost frame; during unwind of
b961095
+   	#5  0x000000305cec68c3 in clone () from /lib64/tls/libc.so.6
b961095
+   avoid the additional bogus frame
b961095
+   	#6  0x0000000000000000 in ??
b961095
+   We compare if the `linux_clone_code' block is _before_ unwound PC.  */
b961095
+
b961095
+static const unsigned char linux_clone_code[] =
b961095
+{
b961095
+/* libc/sysdeps/unix/sysv/linux/x86_64/clone.S */
b961095
+/* #ifdef RESET_PID */
b961095
+/* ... */
b961095
+/* 	mov	$SYS_ify(getpid), %eax */
b961095
+/* 0xb8, 0x27, 0x00, 0x00, 0x00 */
b961095
+/* OR */
b961095
+/* 	mov	$SYS_ify(getpid), %rax */
b961095
+/* 0x48, 0xc7, 0xc0, 0x27, 0x00, 0x00, 0x00 */
b961095
+/* so just: */
b961095
+  0x27, 0x00, 0x00, 0x00,
b961095
+/* 	syscall */
b961095
+  0x0f, 0x05,
b961095
+/* 	movl	%eax, %fs:PID */
b961095
+  0x64, 0x89, 0x04, 0x25, 0x94, 0x00, 0x00, 0x00,
b961095
+/* 	movl	%eax, %fs:TID */
b961095
+  0x64, 0x89, 0x04, 0x25, 0x90, 0x00, 0x00, 0x00,
b961095
+/* #endif */
b961095
+/* 	|* Set up arguments for the function call.  *| */
b961095
+/* 	popq	%rax		|* Function to call.  *| */
b961095
+  0x58,
b961095
+/* 	popq	%rdi		|* Argument.  *| */
b961095
+  0x5f,
b961095
+/* 	call	*%rax$   */
b961095
+  0xff, 0xd0
b961095
+};
b961095
+
b961095
+#define LINUX_CLONE_LEN (sizeof linux_clone_code)
b961095
+
b961095
+static int
407ebe9
+amd64_linux_clone_running (struct frame_info *this_frame)
b961095
+{
407ebe9
+  CORE_ADDR pc = get_frame_pc (this_frame);
b961095
+  unsigned char buf[LINUX_CLONE_LEN];
b961095
+
407ebe9
+  if (!safe_frame_unwind_memory (this_frame, pc - LINUX_CLONE_LEN, buf,
b961095
+				 LINUX_CLONE_LEN))
b961095
+    return 0;
b961095
+
b961095
+  if (memcmp (buf, linux_clone_code, LINUX_CLONE_LEN) != 0)
b961095
+    return 0;
b961095
+
b961095
+  return 1;
b961095
+}
b961095
+
b961095
+static int
407ebe9
+amd64_linux_outermost_frame (struct frame_info *this_frame)
b961095
+{
407ebe9
+  CORE_ADDR pc = get_frame_pc (this_frame);
b961095
+  char *name;
b961095
+
b961095
+  find_pc_partial_function (pc, &name, NULL, NULL);
b961095
+
b961095
+  /* If we have NAME, we can optimize the search.
b961095
+     `clone' NAME still needs to have the code checked as its name may be
b961095
+     present in the user code.
b961095
+     `__clone' NAME should not be present in the user code but in the initial
b961095
+     parts of the `__clone' implementation the unwind still makes sense.
b961095
+     More detailed unwinding decision would be too much sensitive to possible
b961095
+     subtle changes in specific glibc revisions.  */
b961095
+  if (name == NULL || strcmp (name, "clone") == 0
b961095
+      || strcmp ("__clone", name) == 0)
407ebe9
+    return (amd64_linux_clone_running (this_frame) != 0);
b961095
+
b961095
+  return 0;
b961095
+}
b961095
+
b961095
 static void
eb9d945
 amd64_linux_write_pc (struct regcache *regcache, CORE_ADDR pc)
b961095
 {
e5611bf
@@ -276,6 +350,8 @@ amd64_linux_init_abi (struct gdbarch_inf
b961095
   tdep->sc_reg_offset = amd64_linux_sc_reg_offset;
b961095
   tdep->sc_num_regs = ARRAY_SIZE (amd64_linux_sc_reg_offset);
b961095
 
b961095
+  tdep->outermost_frame_p = amd64_linux_outermost_frame;
b961095
+
b961095
   /* GNU/Linux uses SVR4-style shared libraries.  */
b961095
   set_solib_svr4_fetch_link_map_offsets
b961095
     (gdbarch, svr4_lp64_fetch_link_map_offsets);
e5611bf
Index: gdb-6.8.50.20090802/gdb/amd64-tdep.c
eb9d945
===================================================================
e5611bf
--- gdb-6.8.50.20090802.orig/gdb/amd64-tdep.c	2009-07-02 19:25:52.000000000 +0200
e5611bf
+++ gdb-6.8.50.20090802/gdb/amd64-tdep.c	2009-08-03 15:50:08.000000000 +0200
e5611bf
@@ -1736,11 +1736,16 @@ amd64_frame_this_id (struct frame_info *
b961095
 {
b961095
   struct amd64_frame_cache *cache =
407ebe9
     amd64_frame_cache (this_frame, this_cache);
407ebe9
+  struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (this_frame));
b961095
 
b961095
   /* This marks the outermost frame.  */
b961095
   if (cache->base == 0)
b961095
     return;
b961095
 
b961095
+  /* Detect OS dependent outermost frames; such as `clone'.  */
407ebe9
+  if (tdep->outermost_frame_p && tdep->outermost_frame_p (this_frame))
b961095
+    return;
b961095
+
b961095
   (*this_id) = frame_id_build (cache->base + 16, cache->pc);
b961095
 }
b961095
 
912c396
--- gdb-6.8.50.20090811/gdb/i386-tdep.c.orig	2009-08-10 05:02:39.000000000 +0200
912c396
+++ gdb-6.8.50.20090811/gdb/i386-tdep.c	2009-08-11 16:33:51.000000000 +0200
912c396
@@ -5432,6 +5432,9 @@ i386_gdbarch_init (struct gdbarch_info i
b961095
   tdep->sc_pc_offset = -1;
b961095
   tdep->sc_sp_offset = -1;
b961095
 
b961095
+  /* Unwinding stops on i386 automatically.  */
b961095
+  tdep->outermost_frame_p = NULL;
b961095
+
912c396
   tdep->record_regmap = i386_record_regmap;
912c396
 
b961095
   /* The format used for `long double' on almost all i386 targets is
912c396
--- gdb-6.8.50.20090811/gdb/i386-tdep.h.orig	2009-08-10 05:02:39.000000000 +0200
912c396
+++ gdb-6.8.50.20090811/gdb/i386-tdep.h	2009-08-11 16:34:08.000000000 +0200
912c396
@@ -120,6 +120,9 @@ struct gdbarch_tdep
e5611bf
   int (*i386_sysenter_record) (struct regcache *regcache);
912c396
   /* Parse syscall args.  */
912c396
   int (*i386_syscall_record) (struct regcache *regcache);
b961095
+
b961095
+  /* Detect OS dependent outermost frames; such as `clone'.  */
407ebe9
+  int (*outermost_frame_p) (struct frame_info *this_frame);
b961095
 };
b961095
 
b961095
 /* Floating-point registers.  */
a259cc2
--- a/gdb/ia64-tdep.c
a259cc2
+++ b/gdb/ia64-tdep.c
a259cc2
@@ -2122,6 +2122,137 @@ static const struct frame_unwind ia64_frame_unwind =
a259cc2
   default_frame_sniffer
a259cc2
 };
a259cc2
 
a259cc2
+/* Detect the outermost frame; during unwind of
a259cc2
+   	#6  0x2000000000347100 in __clone2 () from /lib/libc.so.6.1
a259cc2
+   avoid the additional bogus frame
a259cc2
+   	#7  0x0000000000000000 in ?? ()  */
a259cc2
+
a259cc2
+static char linux_clone2_code[] =
a259cc2
+{
a259cc2
+/* libc/sysdeps/unix/sysv/linux/ia64/clone2.S */
a259cc2
+  0x09, 0x00, 0x20, 0x12, 0x90, 0x11, 0x00, 0x40,
a259cc2
+  0x28, 0x20, 0x23, 0x00, 0x00, 0x00, 0x04, 0x00,
a259cc2
+/*         st4 [r9]=r8 */
a259cc2
+/*         st4 [r10]=r8 */
a259cc2
+/*         ;; */
a259cc2
+/* #endif */
a259cc2
+  0x02, 0x50, 0x21, 0x40, 0x18, 0x14, 0x90, 0x02,
a259cc2
+  0x90, 0x00, 0x42, 0x00, 0x00, 0x00, 0x04, 0x00,
a259cc2
+/* 1:      ld8 out1=[in0],8        |* Retrieve code pointer.       *| */
a259cc2
+/*         mov out0=in4            |* Pass proper argument to fn *| */
a259cc2
+/*         ;; */
a259cc2
+  0x11, 0x08, 0x00, 0x40, 0x18, 0x10, 0x60, 0x50,
a259cc2
+  0x05, 0x80, 0x03, 0x00, 0x68, 0x00, 0x80, 0x12,
a259cc2
+/*         ld8 gp=[in0]            |* Load function gp.            *| */
a259cc2
+/*         mov b6=out1 */
a259cc2
+/*         br.call.dptk.many rp=b6 |* Call fn(arg) in the child    *| */
a259cc2
+/*         ;; */
a259cc2
+  0x10, 0x48, 0x01, 0x10, 0x00, 0x21, 0x10, 0x00,
a259cc2
+  0xa0, 0x00, 0x42, 0x00, 0x98, 0xdf, 0xf7, 0x5b,
a259cc2
+/*         mov out0=r8             |* Argument to _exit            *| */
a259cc2
+/*         mov gp=loc0 */
a259cc2
+/*         .globl HIDDEN_JUMPTARGET(_exit) */
a259cc2
+/*         br.call.dpnt.many rp=HIDDEN_JUMPTARGET(_exit) */
a259cc2
+/*                                 |* call _exit with result from fn.      *| */
a259cc2
+  0x11, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
a259cc2
+  0x00, 0x02, 0x00, 0x80, 0x00, 0x00, 0x84, 0x00
a259cc2
+/*         ret                     |* Not reached.         *| */
a259cc2
+};
a259cc2
+
a259cc2
+#define LINUX_CLONE_PRE_SLOTS 3	/* Number of slots before PC.  */
a259cc2
+#define LINUX_CLONE_LEN (sizeof linux_clone2_code)
a259cc2
+
a259cc2
+static int
a259cc2
+ia64_linux_clone2_running (struct frame_info *this_frame)
a259cc2
+{
a259cc2
+  CORE_ADDR pc = get_frame_pc (this_frame);
a259cc2
+  char buf[LINUX_CLONE_LEN];
a259cc2
+  struct minimal_symbol *minsym;
a259cc2
+  long long instr;
a259cc2
+
a259cc2
+  if (!safe_frame_unwind_memory (this_frame, pc - LINUX_CLONE_PRE_SLOTS * 16,
a259cc2
+				 buf, LINUX_CLONE_LEN))
a259cc2
+    return 0;
a259cc2
+
a259cc2
+  if (memcmp (buf, linux_clone2_code, LINUX_CLONE_PRE_SLOTS * 16) != 0)
a259cc2
+    return 0;
a259cc2
+
a259cc2
+  /* Adjust the expected "_exit" address.  */
a259cc2
+  minsym = lookup_minimal_symbol_text ("_exit", NULL);
a259cc2
+  if (minsym == NULL)
a259cc2
+    return 0;
a259cc2
+
a259cc2
+  instr = slotN_contents (&buf[LINUX_CLONE_PRE_SLOTS * 16], 2);
a259cc2
+  instr &= ~(((1L << 20) - 1) << 13);
a259cc2
+  /* Address is relative to the jump instruction slot, not the next one.  */
a259cc2
+  instr |= (((SYMBOL_VALUE_ADDRESS (minsym) - (pc & ~0xfL)) >> 4)
a259cc2
+	    & ((1L << 20) - 1)) << 13;
a259cc2
+  replace_slotN_contents (&buf[LINUX_CLONE_PRE_SLOTS * 16], instr, 2);
a259cc2
+
a259cc2
+  if (memcmp (&buf[LINUX_CLONE_PRE_SLOTS * 16],
a259cc2
+              &linux_clone2_code[LINUX_CLONE_PRE_SLOTS * 16],
a259cc2
+	      LINUX_CLONE_LEN - (LINUX_CLONE_PRE_SLOTS * 16)) != 0)
a259cc2
+    return 0;
a259cc2
+
a259cc2
+  return 1;
a259cc2
+}
a259cc2
+
a259cc2
+static int
a259cc2
+ia64_outermost_frame (struct frame_info *this_frame)
a259cc2
+{
a259cc2
+  CORE_ADDR pc = get_frame_pc (this_frame);
a259cc2
+  char *name;
a259cc2
+
a259cc2
+  find_pc_partial_function (pc, &name, NULL, NULL);
a259cc2
+
a259cc2
+  /* If we have NAME, we can optimize the search.
a259cc2
+     `clone' NAME still needs to have the code checked as its name may be
a259cc2
+     present in the user code.
a259cc2
+     `__clone' NAME should not be present in the user code but in the initial
a259cc2
+     parts of the `__clone' implementation the unwind still makes sense.
a259cc2
+     More detailed unwinding decision would be too much sensitive to possible
a259cc2
+     subtle changes in specific glibc revisions.  */
a259cc2
+  if (name == NULL || strcmp (name, "clone2") == 0
a259cc2
+      || strcmp ("__clone2", name) == 0)
a259cc2
+    return (ia64_linux_clone2_running (this_frame) != 0);
a259cc2
+
a259cc2
+  return 0;
a259cc2
+}
a259cc2
+
a259cc2
+static void
a259cc2
+ia64_clone2_frame_this_id (struct frame_info *this_frame, void **this_cache,
a259cc2
+			   struct frame_id *this_id)
a259cc2
+{
a259cc2
+  /* Leave the default outermost frame at *THIS_ID.  */
a259cc2
+}
a259cc2
+
a259cc2
+static struct value *
a259cc2
+ia64_clone2_frame_prev_register (struct frame_info *this_frame,
a259cc2
+				 void **this_cache, int regnum)
a259cc2
+{
a259cc2
+  return frame_unwind_got_register (this_frame, regnum, regnum);
a259cc2
+}
a259cc2
+
a259cc2
+static int
a259cc2
+ia64_clone2_frame_sniffer (const struct frame_unwind *self,
a259cc2
+			   struct frame_info *this_frame,
a259cc2
+			   void **this_prologue_cache)
a259cc2
+{
a259cc2
+  if (ia64_outermost_frame (this_frame))
a259cc2
+    return 1;
a259cc2
+
a259cc2
+  return 0;
a259cc2
+}
a259cc2
+
a259cc2
+static const struct frame_unwind ia64_clone2_frame_unwind =
a259cc2
+{
a259cc2
+  NORMAL_FRAME,
a259cc2
+  &ia64_clone2_frame_this_id,
a259cc2
+  &ia64_clone2_frame_prev_register,
a259cc2
+  NULL,
a259cc2
+  &ia64_clone2_frame_sniffer
a259cc2
+};
a259cc2
+
a259cc2
 /* Signal trampolines.  */
a259cc2
 
a259cc2
 static void
a259cc2
@@ -3824,6 +3955,7 @@ ia64_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
a259cc2
   set_gdbarch_dummy_id (gdbarch, ia64_dummy_id);
a259cc2
 
a259cc2
   set_gdbarch_unwind_pc (gdbarch, ia64_unwind_pc);
a259cc2
+  frame_unwind_append_unwinder (gdbarch, &ia64_clone2_frame_unwind);
a259cc2
 #ifdef HAVE_LIBUNWIND_IA64_H
a259cc2
   frame_unwind_append_unwinder (gdbarch,
a259cc2
                                 &ia64_libunwind_sigtramp_frame_unwind);
e5611bf
Index: gdb-6.8.50.20090802/gdb/testsuite/gdb.threads/bt-clone-stop.c
eb9d945
===================================================================
eb9d945
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
e5611bf
+++ gdb-6.8.50.20090802/gdb/testsuite/gdb.threads/bt-clone-stop.c	2009-08-03 15:50:08.000000000 +0200
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
+}
e5611bf
Index: gdb-6.8.50.20090802/gdb/testsuite/gdb.threads/bt-clone-stop.exp
eb9d945
===================================================================
eb9d945
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
e5611bf
+++ gdb-6.8.50.20090802/gdb/testsuite/gdb.threads/bt-clone-stop.exp	2009-08-03 15:50:08.000000000 +0200
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
b961095
+set binfile ${objdir}/${subdir}/${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" \
b961095
+     "Program 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
+}