1 /* This testcase is part of GDB, the GNU debugger.
3 Copyright 2009-2024 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 /* Number of watchpoints GDB is capable of using (this is provided
51 by GDB during the test run). */
52 int hw_watch_count
= 0;
54 /* Array with elements we can create watchpoints for. */
55 static int watched_data
[NR_THREADS
];
56 pthread_mutex_t data_mutex
;
62 pthread_t threads
[NR_THREADS
];
65 pthread_mutex_init (&data_mutex
, NULL
);
67 for (i
= 0; i
< NR_THREADS
; i
++)
69 res
= pthread_create (&threads
[i
],
70 NULL
, thread_function
,
71 (void *) (intptr_t) i
);
74 fprintf (stderr
, "error in thread %d create\n", i
);
79 for (i
= 0; i
< NR_THREADS
; ++i
)
81 res
= pthread_join (threads
[i
], NULL
);
84 fprintf (stderr
, "error in thread %d join\n", i
);
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. */
101 thread_started (void)
106 thread_function (void *arg
)
109 long thread_number
= (long) arg
;
113 /* Don't start incrementing X until wp-replication.exp is ready. */
117 pthread_mutex_lock (&data_mutex
);
119 for (i
= 0; i
< NR_TRIGGERS_PER_THREAD
; i
++)
121 for (j
= 0; j
< hw_watch_count
; j
++)
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. */
132 pthread_mutex_unlock (&data_mutex
);
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
139 while (!can_terminate
)