5a86319
2005-02-28  Jeff Johnston  <jjohnstn@redhat.com>
5a86319
5a86319
	* config/i386/nm-linux.h: Change dr register routines to
5a86319
	accept a ptid_t first argument.  Change all calling macros
5a86319
	to default the inferior_ptid for the first argument.
5a86319
	(i386_linux_insert_watchpoint): New prototype.
5a86319
	(i386_linux_remove_watchpoint, i386_linux_insert_hw_breakpoint): Ditto.
5a86319
	(i386_linux_remove_hw_breakpoint): Ditto.
5a86319
	(target_insert_watchpoint, target_remove_watchpoint): Undef and
5a86319
	override.
5a86319
	(target_insert_hw_breakpoint, target_remove_hw_breakpoint): Ditto.
5a86319
	* config/i386/nm-linux64.h: Ditto except add amd64 versions of
5a86319
	the watchpoint/hw-breakpoint insert/remove routines.
5a86319
	* i386-nat.c: Include "inferior.h" to define inferior_ptid.
5a86319
	* i386-linux-nat.c: Change all dr get/set routines to accept
5a86319
	ptid_t as first argument and to use this argument to determine
5a86319
	the tid for PTRACE.
5a86319
	(i386_linux_set_debug_regs_for_thread): New function.
5a86319
	(i386_linux_sync_debug_registers_callback): Ditto.
5a86319
	(i386_linux_sync_debug_registers_across_threads): Ditto.
5a86319
	(i386_linux_insert_watchpoint, i386_linux_remove_watchpoint): Ditto.
5a86319
	(i386_linux_hw_breakpoint, i386_linux_remove_hw_breakpoint): Ditto.
5a86319
	(i386_linux_new_thread): Ditto.
5a86319
	(_initialize_i386_linux_nat): Ditto.
5a86319
	* amd64-linux-nat.c: Change all dr get/set routines to accept
5a86319
	ptid_t as first argument and to use this argument to determine
5a86319
	the tid for PTRACE.
5a86319
	(amd64_linux_set_debug_regs_for_thread): New function.
5a86319
	(amd64_linux_sync_debug_registers_callback): Ditto.
5a86319
	(amd64_linux_sync_debug_registers_across_threads): Ditto.
5a86319
	(amd64_linux_insert_watchpoint, amd64_linux_remove_watchpoint): Ditto.
5a86319
	(amd64_linux_hw_breakpoint, amd64_linux_remove_hw_breakpoint): Ditto.
5a86319
	(amd64_linux_new_thread): Ditto.
5a86319
	(_initialize_amd64_linux_nat): Register linux new thread observer.
5a86319
	* testsuite/gdb.threads/watchthreads2.c: New test case.
5a86319
	* testsuite/gdb.threads/watchthreads2.exp: Ditto.
5a86319
5a86319
[ With recent upstream GDB (6.8) reduced only to the testcase.  ]
5a86319
1a96385
FIXME: The testcase does not expects multiple watchpoints hits per one stop.
1a96385
5a86319
Index: gdb-6.5/gdb/testsuite/gdb.threads/watchthreads2.c
5a86319
===================================================================
5a86319
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
5a86319
+++ gdb-6.5/gdb/testsuite/gdb.threads/watchthreads2.c	2006-07-12 01:54:29.000000000 -0300
5a86319
@@ -0,0 +1,66 @@
5a86319
+/* This testcase is part of GDB, the GNU debugger.
5a86319
+
5a86319
+   Copyright 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5a86319
+
5a86319
+   This program is free software; you can redistribute it and/or modify
5a86319
+   it under the terms of the GNU General Public License as published by
5a86319
+   the Free Software Foundation; either version 2 of the License, or
5a86319
+   (at your option) any later version.
5a86319
+
5a86319
+   This program is distributed in the hope that it will be useful,
5a86319
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
5a86319
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5a86319
+   GNU General Public License for more details.
5a86319
+
5a86319
+   You should have received a copy of the GNU General Public License
5a86319
+   along with this program; if not, write to the Free Software
5a86319
+   Foundation, Inc., 59 Temple Place - Suite 330,
5a86319
+   Boston, MA 02111-1307, USA.  
5a86319
+ 
5a86319
+   This file is copied from schedlock.c.  */
5a86319
+
5a86319
+#include <stdio.h>
5a86319
+#include <unistd.h>
5a86319
+#include <stdlib.h>
5a86319
+#include <pthread.h>
5a86319
+
5a86319
+void *thread_function(void *arg); /* Pointer to function executed by each thread */
5a86319
+
5a86319
+#define NUM 5
5a86319
+
5a86319
+unsigned int args[NUM+1];
5a86319
+
5a86319
+int main() {
5a86319
+    int res;
5a86319
+    pthread_t threads[NUM];
5a86319
+    void *thread_result;
5a86319
+    long i;
5a86319
+
5a86319
+    for (i = 0; i < NUM; i++)
5a86319
+      {
5a86319
+	args[i] = 1; /* Init value.  */
5a86319
+	res = pthread_create(&threads[i],
5a86319
+		             NULL,
5a86319
+			     thread_function,
5a86319
+			     (void *) i);
5a86319
+      }
5a86319
+
5a86319
+    args[i] = 1;
5a86319
+    thread_function ((void *) i);
5a86319
+
5a86319
+    exit(EXIT_SUCCESS);
5a86319
+}
5a86319
+
5a86319
+void *thread_function(void *arg) {
5a86319
+    int my_number =  (long) arg;
5a86319
+    int *myp = (int *) &args[my_number];
5a86319
+
5a86319
+    /* Don't run forever.  Run just short of it :)  */
5a86319
+    while (*myp > 0)
5a86319
+      {
5a86319
+	(*myp) ++; usleep (1); /* Loop increment.  */
5a86319
+      }
5a86319
+
5a86319
+    pthread_exit(NULL);
5a86319
+}
5a86319
+
5a86319
Index: gdb-6.5/gdb/testsuite/gdb.threads/watchthreads2.exp
5a86319
===================================================================
5a86319
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
5a86319
+++ gdb-6.5/gdb/testsuite/gdb.threads/watchthreads2.exp	2006-07-12 01:54:29.000000000 -0300
5a86319
@@ -0,0 +1,133 @@
5a86319
+# This testcase is part of GDB, the GNU debugger.
5a86319
+
5a86319
+# Copyright 2005 Free Software Foundation, Inc.
5a86319
+
5a86319
+# This program is free software; you can redistribute it and/or modify
5a86319
+# it under the terms of the GNU General Public License as published by
5a86319
+# the Free Software Foundation; either version 2 of the License, or
5a86319
+# (at your option) any later version.
5a86319
+#
5a86319
+# This program is distributed in the hope that it will be useful,
5a86319
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
5a86319
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5a86319
+# GNU General Public License for more details.
5a86319
+#
5a86319
+# You should have received a copy of the GNU General Public License
5a86319
+# along with this program; if not, write to the Free Software
5a86319
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  
5a86319
+
5a86319
+# Check that GDB can support multiple watchpoints across threads.
5a86319
+
5a86319
+if $tracelevel {
5a86319
+    strace $tracelevel
5a86319
+}
5a86319
+
5a86319
+set prms_id 0
5a86319
+set bug_id 0
5a86319
+
5a86319
+# This test verifies that a watchpoint is detected in the proper thread
5a86319
+# so the test is only meaningful on a system with hardware watchpoints.
5a86319
+if [target_info exists gdb,no_hardware_watchpoints] {
5a86319
+    return 0;
5a86319
+}
5a86319
+
5a86319
+set testfile "watchthreads2"
5a86319
+set srcfile ${testfile}.c
5a86319
+set binfile ${objdir}/${subdir}/${testfile}
5a86319
+if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable [list debug "incdir=${objdir}"]] != "" } {
5a86319
+    return -1
5a86319
+}
5a86319
+
5a86319
+gdb_exit
5a86319
+gdb_start
5a86319
+gdb_reinitialize_dir $srcdir/$subdir
5a86319
+gdb_load ${binfile}
5a86319
+
5a86319
+gdb_test "set can-use-hw-watchpoints 1" "" ""
5a86319
+
5a86319
+#
5a86319
+# Run to `main' where we begin our tests.
5a86319
+#
5a86319
+
5a86319
+if ![runto_main] then {
5a86319
+    gdb_suppress_tests
5a86319
+}
5a86319
+
5a86319
+set args_2 0
5a86319
+set args_3 0
5a86319
+
5a86319
+gdb_breakpoint "thread_function"
5a86319
+gdb_continue_to_breakpoint "thread_function"
5a86319
+gdb_test "disable 2" ""
5a86319
+
5a86319
+gdb_test_multiple "p args\[2\]" "get initial args2" {
5a86319
+  -re "\\\$\[0-9\]* = (.*)$gdb_prompt $" {
5a86319
+    set init_args_2 $expect_out(1,string)
5a86319
+    pass "get initial args2"
5a86319
+  }
5a86319
+}
5a86319
+
5a86319
+gdb_test_multiple "p args\[3\]" "get initial args3" {
5a86319
+  -re "\\\$\[0-9\]* = (.*)$gdb_prompt $" {
5a86319
+    set init_args_3 $expect_out(1,string)
5a86319
+    pass "get initial args3"
5a86319
+  }
5a86319
+}
5a86319
+
5a86319
+set args_2 $init_args_2
5a86319
+set args_3 $init_args_3
5a86319
+
5a86319
+# Watch values that will be modified by distinct threads.
5a86319
+gdb_test "watch args\[2\]" "Hardware watchpoint 3: args\\\[2\\\]"
5a86319
+gdb_test "watch args\[3\]" "Hardware watchpoint 4: args\\\[3\\\]"
5a86319
+
5a86319
+set init_line [expr [gdb_get_line_number "Init value"]+1]
5a86319
+set inc_line [gdb_get_line_number "Loop increment"]
5a86319
+
5a86319
+# Loop and continue to allow both watchpoints to be triggered.
5a86319
+for {set i 0} {$i < 30} {incr i} {
5a86319
+  set test_flag 0
5a86319
+  gdb_test_multiple "continue" "threaded watch loop" {
5a86319
+    -re "Hardware watchpoint 3: args\\\[2\\\].*Old value = 0.*New value = 1.*main \\\(\\\) at .*watchthreads2.c:$init_line.*$gdb_prompt $"
5a86319
+       { set args_2 1; set test_flag 1 }
5a86319
+    -re "Hardware watchpoint 4: args\\\[3\\\].*Old value = 0.*New value = 1.*main \\\(\\\) at .*watchthreads2.c:$init_line.*$gdb_prompt $"
5a86319
+       { set args_3 1; set test_flag 1 }
5a86319
+    -re "Hardware watchpoint 3: args\\\[2\\\].*Old value = $args_2.*New value = [expr $args_2+1].*in thread_function \\\(arg=0x2\\\) at .*watchthreads2.c:$inc_line.*$gdb_prompt $"
5a86319
+       { set args_2 [expr $args_2+1]; set test_flag 1 }
5a86319
+    -re "Hardware watchpoint 4: args\\\[3\\\].*Old value = $args_3.*New value = [expr $args_3+1].*in thread_function \\\(arg=0x3\\\) at .*watchthreads2.c:$inc_line.*$gdb_prompt $"
5a86319
+       { set args_3 [expr $args_3+1]; set test_flag 1 }
5a86319
+  }
5a86319
+  # If we fail above, don't bother continuing loop
5a86319
+  if { $test_flag == 0 } {
5a86319
+    set i 30;
5a86319
+  }
5a86319
+}
5a86319
+
5a86319
+# Print success message if loop succeeded.
5a86319
+if { $test_flag == 1 } {
5a86319
+  pass "threaded watch loop"
5a86319
+}
5a86319
+
5a86319
+# Verify that we hit first watchpoint in child thread.
5a86319
+set message "watchpoint on args\[2\] hit in thread"
5a86319
+if { $args_2 > 1 } {
5a86319
+  pass $message 
5a86319
+} else {
5a86319
+  fail $message
5a86319
+}
5a86319
+
5a86319
+# Verify that we hit second watchpoint in child thread.
5a86319
+set message "watchpoint on args\[3\] hit in thread"
5a86319
+if { $args_3 > 1 } {
5a86319
+  pass $message 
5a86319
+} else {
5a86319
+  fail $message 
5a86319
+}
5a86319
+
5a86319
+# Verify that all watchpoint hits are accounted for.
5a86319
+set message "combination of threaded watchpoints = 30 + initial values"
5a86319
+if { [expr $args_2+$args_3] == [expr [expr 30+$init_args_2]+$init_args_3] } {
5a86319
+  pass $message 
5a86319
+} else {
5a86319
+  fail $message 
5a86319
+}