b961095
https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=216711
b961095
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
b961095
b961095
--- ./gdb/amd64-linux-tdep.c	19 Aug 2006 15:15:18 -0000	1.12
b961095
+++ ./gdb/amd64-linux-tdep.c	17 Dec 2006 17:59:05 -0000
b961095
@@ -235,6 +235,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
b961095
+amd64_linux_clone_running (struct frame_info *next_frame)
b961095
+{
b961095
+  CORE_ADDR pc = frame_pc_unwind (next_frame);
b961095
+  unsigned char buf[LINUX_CLONE_LEN];
b961095
+
b961095
+  if (!safe_frame_unwind_memory (next_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
b961095
+amd64_linux_outermost_frame (struct frame_info *next_frame)
b961095
+{
b961095
+  CORE_ADDR pc = frame_pc_unwind (next_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)
b961095
+    return (amd64_linux_clone_running (next_frame) != 0);
b961095
+
b961095
+  return 0;
b961095
+}
b961095
+
b961095
 static void
b961095
 amd64_linux_write_pc (CORE_ADDR pc, ptid_t ptid)
b961095
 {
b961095
@@ -273,6 +342,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);
b961095
--- ./gdb/amd64-tdep.c	19 Aug 2006 15:15:18 -0000	1.32
b961095
+++ ./gdb/amd64-tdep.c	17 Dec 2006 17:59:05 -0000
b961095
@@ -879,11 +879,16 @@ amd64_frame_this_id (struct frame_info *
b961095
 {
b961095
   struct amd64_frame_cache *cache =
b961095
     amd64_frame_cache (next_frame, this_cache);
b961095
+  struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
b961095
 
b961095
   /* This marks the outermost frame.  */
b961095
   if (cache->base == 0)
b961095
     return;
b961095
 
b961095
+  /* Detect OS dependent outermost frames; such as `clone'.  */
b961095
+  if (tdep->outermost_frame_p && tdep->outermost_frame_p (next_frame))
b961095
+    return;
b961095
+
b961095
   (*this_id) = frame_id_build (cache->base + 16, cache->pc);
b961095
 }
b961095
 
b961095
--- ./gdb/i386-tdep.c	8 Aug 2006 21:36:46 -0000	1.225
b961095
+++ ./gdb/i386-tdep.c	17 Dec 2006 17:59:10 -0000
b961095
@@ -2313,6 +2313,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
+
b961095
   /* The format used for `long double' on almost all i386 targets is
b961095
      the i387 extended floating-point format.  In fact, of all targets
b961095
      in the GCC 2.95 tree, only OSF/1 does it different, and insists
b961095
--- ./gdb/i386-tdep.h	21 Jan 2006 20:59:50 -0000	1.46
b961095
+++ ./gdb/i386-tdep.h	17 Dec 2006 17:59:10 -0000
b961095
@@ -104,6 +104,9 @@ struct gdbarch_tdep
b961095
      is deprecated, please use `sc_reg_offset' instead.  */
b961095
   int sc_pc_offset;
b961095
   int sc_sp_offset;
b961095
+
b961095
+  /* Detect OS dependent outermost frames; such as `clone'.  */
b961095
+  int (*outermost_frame_p) (struct frame_info *next_frame);
b961095
 };
b961095
 
b961095
 /* Floating-point registers.  */
b961095
--- /dev/null	1 Jan 1970 00:00:00 -0000
b961095
+++ ./gdb/testsuite/gdb.threads/bt-clone-stop.c	17 Dec 2006 17:59:19 -0000
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
+}
b961095
--- /dev/null	1 Jan 1970 00:00:00 -0000
b961095
+++ ./gdb/testsuite/gdb.threads/bt-clone-stop.exp	17 Dec 2006 17:59:19 -0000
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
+}