1 /* This testcase is part of GDB, the GNU debugger.
3 Copyright 2009-2015 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 Check that hardware watchpoints get correctly replicated to all
19 existing threads when hardware watchpoints are created. This test
20 creates one hardware watchpoint per thread until a maximum is
21 reached. It originally addresses a deficiency seen on embedded
22 powerpc targets with slotted hardware *point designs.
32 #define NR_THREADS 4 /* Set by the testcase. */
36 #define X_INCR_COUNT 10 /* Set by the testcase. */
39 void *thread_function (void *arg
); /* Function executed by each thread. */
41 /* Used to hold threads back until wp-replication.exp is ready. */
44 /* Used to hold threads back until every thread has had a chance of causing
45 a watchpoint trigger. This prevents a situation in GDB where it may miss
46 watchpoint triggers when threads exit while other threads are causing
47 watchpoint triggers. */
48 int can_terminate
= 0;
50 /* Used to push the program out of the waiting loop after the
51 testcase is done counting the number of hardware watchpoints
52 available for our target. */
53 int watch_count_done
= 0;
55 /* Number of watchpoints GDB is capable of using (this is provided
56 by GDB during the test run). */
57 int hw_watch_count
= 0;
59 /* Array with elements we can create watchpoints for. */
60 static int watched_data
[NR_THREADS
];
61 pthread_mutex_t data_mutex
;
63 /* Wait function to keep threads busy while the testcase does
64 what it needs to do. */
75 pthread_t threads
[NR_THREADS
];
78 /* Something to ensure that the breakpoint used to run to main
82 while (watch_count_done
== 0)
84 /* GDB will modify the value of "watch_count_done" at runtime and we
85 will get past this point. */
89 pthread_mutex_init (&data_mutex
, NULL
);
91 for (i
= 0; i
< NR_THREADS
; i
++)
93 res
= pthread_create (&threads
[i
],
94 NULL
, thread_function
,
95 (void *) (intptr_t) i
);
98 fprintf (stderr
, "error in thread %d create\n", i
);
103 for (i
= 0; i
< NR_THREADS
; ++i
)
105 res
= pthread_join (threads
[i
], NULL
);
108 fprintf (stderr
, "error in thread %d join\n", i
);
116 /* Easy place for a breakpoint.
117 wp-replication.exp uses this to track when all threads are running
118 instead of, for example, the program keeping track
119 because we don't need the program to know when all threads are running,
120 instead we need gdb to know when all threads are running.
121 There is a delay between when a thread has started and when the thread
122 has been registered with gdb. */
125 thread_started (void)
130 thread_function (void *arg
)
133 long thread_number
= (long) arg
;
137 /* Don't start incrementing X until wp-replication.exp is ready. */
141 pthread_mutex_lock (&data_mutex
);
143 for (i
= 0; i
< NR_TRIGGERS_PER_THREAD
; i
++)
145 for (j
= 0; j
< hw_watch_count
; j
++)
148 printf ("Thread %ld changing watch_thread[%d] data"
149 " from %d -> %d\n", thread_number
, j
,
150 watched_data
[j
], watched_data
[j
] + 1);
151 /* Increment the watched data field. */
156 pthread_mutex_unlock (&data_mutex
);
158 /* Hold the threads here to work around a problem GDB has evaluating
159 watchpoints right when a DSO event shows up (PR breakpoints/10116).
160 Sleep a little longer (than, say, 1, 5 or 10) to avoid consuming
161 lots of cycles while the other threads are trying to execute the
163 while (!can_terminate
)