gdb/
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.threads / wp-replication.c
1 /* This testcase is part of GDB, the GNU debugger.
2
3 Copyright 2009-2013 Free Software Foundation, Inc.
4
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.
9
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.
14
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/>.
17
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.
23 */
24
25 #include <stdio.h>
26 #include <unistd.h>
27 #include <stdlib.h>
28 #include <stdint.h>
29 #include <pthread.h>
30
31 #ifndef NR_THREADS
32 #define NR_THREADS 4 /* Set by the testcase. */
33 #endif
34
35 #ifndef X_INCR_COUNT
36 #define X_INCR_COUNT 10 /* Set by the testcase. */
37 #endif
38
39 void *thread_function (void *arg); /* Function executed by each thread. */
40
41 /* Used to hold threads back until wp-replication.exp is ready. */
42 int test_ready = 0;
43
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;
49
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;
54
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;
58
59 /* Array with elements we can create watchpoints for. */
60 static int watched_data[NR_THREADS];
61 pthread_mutex_t data_mutex;
62
63 /* Wait function to keep threads busy while the testcase does
64 what it needs to do. */
65 void
66 empty_cycle (void)
67 {
68 usleep (1);
69 }
70
71 int
72 main ()
73 {
74 int res;
75 pthread_t threads[NR_THREADS];
76 int i;
77
78 while (watch_count_done == 0)
79 {
80 /* GDB will modify the value of "i" at runtime and we will
81 get past this point. */
82 empty_cycle ();
83 }
84
85 pthread_mutex_init (&data_mutex, NULL);
86
87 for (i = 0; i < NR_THREADS; i++)
88 {
89 res = pthread_create (&threads[i],
90 NULL, thread_function,
91 (void *) (intptr_t) i);
92 if (res != 0)
93 {
94 fprintf (stderr, "error in thread %d create\n", i);
95 abort ();
96 }
97 }
98
99 for (i = 0; i < NR_THREADS; ++i)
100 {
101 res = pthread_join (threads[i], NULL);
102 if (res != 0)
103 {
104 fprintf (stderr, "error in thread %d join\n", i);
105 abort ();
106 }
107 }
108
109 exit (EXIT_SUCCESS);
110 }
111
112 /* Easy place for a breakpoint.
113 wp-replication.exp uses this to track when all threads are running
114 instead of, for example, the program keeping track
115 because we don't need the program to know when all threads are running,
116 instead we need gdb to know when all threads are running.
117 There is a delay between when a thread has started and when the thread
118 has been registered with gdb. */
119
120 void
121 thread_started (void)
122 {
123 }
124
125 void *
126 thread_function (void *arg)
127 {
128 int i, j;
129 long thread_number = (long) arg;
130
131 thread_started ();
132
133 /* Don't start incrementing X until wp-replication.exp is ready. */
134 while (!test_ready)
135 usleep (1);
136
137 pthread_mutex_lock (&data_mutex);
138
139 for (i = 0; i < NR_TRIGGERS_PER_THREAD; i++)
140 {
141 for (j = 0; j < hw_watch_count; j++)
142 {
143 /* For debugging. */
144 printf ("Thread %ld changing watch_thread[%d] data"
145 " from %d -> %d\n", thread_number, j,
146 watched_data[j], watched_data[j] + 1);
147 /* Increment the watched data field. */
148 watched_data[j]++;
149 }
150 }
151
152 pthread_mutex_unlock (&data_mutex);
153
154 /* Hold the threads here to work around a problem GDB has evaluating
155 watchpoints right when a DSO event shows up (PR breakpoints/10116).
156 Sleep a little longer (than, say, 1, 5 or 10) to avoid consuming
157 lots of cycles while the other threads are trying to execute the
158 loop. */
159 while (!can_terminate)
160 usleep (100);
161
162 pthread_exit (NULL);
163 }
This page took 0.075738 seconds and 5 git commands to generate.