update copyright year range in GDB files
[external/binutils.git] / gdb / testsuite / gdb.threads / wp-replication.c
1 /* This testcase is part of GDB, the GNU debugger.
2
3    Copyright 2009-2017 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 /* Number of watchpoints GDB is capable of using (this is provided
51    by GDB during the test run).  */
52 int hw_watch_count = 0;
53
54 /* Array with elements we can create watchpoints for.  */
55 static int watched_data[NR_THREADS];
56 pthread_mutex_t data_mutex;
57
58 int
59 main ()
60 {
61   int res;
62   pthread_t threads[NR_THREADS];
63   int i;
64
65   pthread_mutex_init (&data_mutex, NULL);
66
67   for (i = 0; i < NR_THREADS; i++)
68     {
69       res = pthread_create (&threads[i],
70                             NULL, thread_function,
71                             (void *) (intptr_t) i);
72       if (res != 0)
73         {
74           fprintf (stderr, "error in thread %d create\n", i);
75           abort ();
76         }
77     }
78
79   for (i = 0; i < NR_THREADS; ++i)
80     {
81       res = pthread_join (threads[i], NULL);
82       if (res != 0)
83         {
84           fprintf (stderr, "error in thread %d join\n", i);
85           abort ();
86         }
87     }
88
89   exit (EXIT_SUCCESS);
90 }
91
92 /* Easy place for a breakpoint.
93    wp-replication.exp uses this to track when all threads are running
94    instead of, for example, the program keeping track
95    because we don't need the program to know when all threads are running,
96    instead we need gdb to know when all threads are running.
97    There is a delay between when a thread has started and when the thread
98    has been registered with gdb.  */
99
100 void
101 thread_started (void)
102 {
103 }
104
105 void *
106 thread_function (void *arg)
107 {
108   int i, j;
109   long thread_number = (long) arg;
110
111   thread_started ();
112
113   /* Don't start incrementing X until wp-replication.exp is ready.  */
114   while (!test_ready)
115     usleep (1);
116
117   pthread_mutex_lock (&data_mutex);
118
119   for (i = 0; i < NR_TRIGGERS_PER_THREAD; i++)
120     {
121       for (j = 0; j < hw_watch_count; j++)
122         {
123           /* For debugging.  */
124           printf ("Thread %ld changing watch_thread[%d] data"
125                   " from %d -> %d\n", thread_number, j,
126                   watched_data[j], watched_data[j] + 1);
127           /* Increment the watched data field.  */
128           watched_data[j]++;
129         }
130     }
131
132   pthread_mutex_unlock (&data_mutex);
133
134   /* Hold the threads here to work around a problem GDB has evaluating
135      watchpoints right when a DSO event shows up (PR breakpoints/10116).
136      Sleep a little longer (than, say, 1, 5 or 10) to avoid consuming
137      lots of cycles while the other threads are trying to execute the
138      loop.  */
139   while (!can_terminate)
140     usleep (100);
141
142   pthread_exit (NULL);
143 }