]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.threads/wp-replication.c
* gdb.threads/wp-replication.c (main): Insert some code at the start
[thirdparty/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 /* Something to ensure that the breakpoint used to run to main
79 is only hit once. */
80 empty_cycle ();
81
82 while (watch_count_done == 0)
83 {
84 /* GDB will modify the value of "watch_count_done" at runtime and we
85 will get past this point. */
86 empty_cycle ();
87 }
88
89 pthread_mutex_init (&data_mutex, NULL);
90
91 for (i = 0; i < NR_THREADS; i++)
92 {
93 res = pthread_create (&threads[i],
94 NULL, thread_function,
95 (void *) (intptr_t) i);
96 if (res != 0)
97 {
98 fprintf (stderr, "error in thread %d create\n", i);
99 abort ();
100 }
101 }
102
103 for (i = 0; i < NR_THREADS; ++i)
104 {
105 res = pthread_join (threads[i], NULL);
106 if (res != 0)
107 {
108 fprintf (stderr, "error in thread %d join\n", i);
109 abort ();
110 }
111 }
112
113 exit (EXIT_SUCCESS);
114 }
115
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. */
123
124 void
125 thread_started (void)
126 {
127 }
128
129 void *
130 thread_function (void *arg)
131 {
132 int i, j;
133 long thread_number = (long) arg;
134
135 thread_started ();
136
137 /* Don't start incrementing X until wp-replication.exp is ready. */
138 while (!test_ready)
139 usleep (1);
140
141 pthread_mutex_lock (&data_mutex);
142
143 for (i = 0; i < NR_TRIGGERS_PER_THREAD; i++)
144 {
145 for (j = 0; j < hw_watch_count; j++)
146 {
147 /* For debugging. */
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. */
152 watched_data[j]++;
153 }
154 }
155
156 pthread_mutex_unlock (&data_mutex);
157
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
162 loop. */
163 while (!can_terminate)
164 usleep (100);
165
166 pthread_exit (NULL);
167 }