1 /* This testcase is part of GDB, the GNU debugger.
3 Copyright 2009-2014 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/>. */
26 #include <sys/types.h>
29 #include <asm/unistd.h>
31 #define gettid() syscall (__NR_gettid)
33 /* Terminate always in the main task, it can lock up with SIGSTOPped GDB
35 #define TIMEOUT (gettid () == getpid() ? 10 : 15)
37 static pid_t thread1_tid
;
38 static pthread_cond_t thread1_tid_cond
= PTHREAD_COND_INITIALIZER
;
39 static pthread_mutex_t thread1_tid_mutex
= PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
;
41 static pid_t thread2_tid
;
42 static pthread_cond_t thread2_tid_cond
= PTHREAD_COND_INITIALIZER
;
43 static pthread_mutex_t thread2_tid_mutex
= PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
;
45 static pthread_mutex_t terminate_mutex
= PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
;
47 /* These variables must have lower in-memory addresses than thread1_rwatch and
48 thread2_rwatch so that they take their watchpoint slots. */
50 static int unused1_rwatch
;
51 static int unused2_rwatch
;
53 static volatile int thread1_rwatch
;
54 static volatile int thread2_rwatch
;
56 /* Do not use alarm as it would create a ptrace event which would hang up us if
57 we are being traced by GDB which we stopped ourselves. */
59 static void timed_mutex_lock (pthread_mutex_t
*mutex
)
62 struct timespec start
, now
;
64 i
= clock_gettime (CLOCK_MONOTONIC
, &start
);
69 i
= pthread_mutex_trylock (mutex
);
74 i
= clock_gettime (CLOCK_MONOTONIC
, &now
);
76 assert (now
.tv_sec
>= start
.tv_sec
);
78 while (now
.tv_sec
- start
.tv_sec
< TIMEOUT
);
80 fprintf (stderr
, "Timed out waiting for internal lock!\n");
85 thread1_func (void *unused
)
88 volatile int rwatch_store
;
90 timed_mutex_lock (&thread1_tid_mutex
);
92 /* THREAD1_TID_MUTEX must be already locked to avoid race. */
93 thread1_tid
= gettid ();
95 i
= pthread_cond_signal (&thread1_tid_cond
);
97 i
= pthread_mutex_unlock (&thread1_tid_mutex
);
100 rwatch_store
= thread1_rwatch
;
102 /* Be sure the "t (tracing stop)" test can proceed for both threads. */
103 timed_mutex_lock (&terminate_mutex
);
104 i
= pthread_mutex_unlock (&terminate_mutex
);
111 thread2_func (void *unused
)
114 volatile int rwatch_store
;
116 timed_mutex_lock (&thread2_tid_mutex
);
118 /* THREAD2_TID_MUTEX must be already locked to avoid race. */
119 thread2_tid
= gettid ();
121 i
= pthread_cond_signal (&thread2_tid_cond
);
123 i
= pthread_mutex_unlock (&thread2_tid_mutex
);
126 rwatch_store
= thread2_rwatch
;
128 /* Be sure the "t (tracing stop)" test can proceed for both threads. */
129 timed_mutex_lock (&terminate_mutex
);
130 i
= pthread_mutex_unlock (&terminate_mutex
);
137 proc_string (const char *filename
, const char *line
)
140 static char buf
[LINE_MAX
];
141 size_t line_len
= strlen (line
);
143 f
= fopen (filename
, "r");
146 fprintf (stderr
, "fopen (\"%s\") for \"%s\": %s\n", filename
, line
,
150 while (errno
= 0, fgets (buf
, sizeof (buf
), f
))
154 s
= strchr (buf
, '\n');
158 if (strncmp (buf
, line
, line_len
) != 0)
163 fprintf (stderr
, "fclose (\"%s\") for \"%s\": %s\n", filename
, line
,
168 return &buf
[line_len
];
172 fprintf (stderr
, "fgets (\"%s\": %s\n", filename
, strerror (errno
));
175 fprintf (stderr
, "\"%s\": No line \"%s\" found.\n", filename
, line
);
180 proc_ulong (const char *filename
, const char *line
)
182 const char *s
= proc_string (filename
, line
);
187 retval
= strtol (s
, &end
, 10);
188 if (retval
< 0 || retval
>= LONG_MAX
|| (end
&& *end
))
190 fprintf (stderr
, "\"%s\":\"%s\": %ld, %s\n", filename
, line
, retval
,
198 state_wait (pid_t process
, const char *wanted
)
202 struct timespec start
, now
;
205 i
= asprintf (&filename
, "/proc/%lu/status", (unsigned long) process
);
208 i
= clock_gettime (CLOCK_MONOTONIC
, &start
);
213 state
= proc_string (filename
, "State:\t");
215 /* torvalds/linux-2.6.git 464763cf1c6df632dccc8f2f4c7e50163154a2c0
216 has changed "T (tracing stop)" to "t (tracing stop)". Make the GDB
217 testcase backward compatible with older Linux kernels. */
218 if (strcmp (state
, "T (tracing stop)") == 0)
219 state
= "t (tracing stop)";
221 if (strcmp (state
, wanted
) == 0)
229 perror ("sched_yield()");
233 i
= clock_gettime (CLOCK_MONOTONIC
, &now
);
235 assert (now
.tv_sec
>= start
.tv_sec
);
237 while (now
.tv_sec
- start
.tv_sec
< TIMEOUT
);
239 fprintf (stderr
, "Timed out waiting for PID %lu \"%s\" (now it is \"%s\")!\n",
240 (unsigned long) process
, wanted
, state
);
244 static volatile pid_t tracer
= 0;
245 static pthread_t thread1
, thread2
;
250 printf ("Resuming GDB PID %lu.\n", (unsigned long) tracer
);
255 int tracer_save
= tracer
;
259 i
= kill (tracer_save
, SIGCONT
);
265 main (int argc
, char **argv
)
270 if (argc
== 2 && strcmp (argv
[1], "-s") == 0)
275 setbuf (stdout
, NULL
);
277 timed_mutex_lock (&thread1_tid_mutex
);
278 timed_mutex_lock (&thread2_tid_mutex
);
280 timed_mutex_lock (&terminate_mutex
);
282 i
= pthread_create (&thread1
, NULL
, thread1_func
, NULL
);
285 i
= pthread_create (&thread2
, NULL
, thread2_func
, NULL
);
290 tracer
= proc_ulong ("/proc/self/status", "TracerPid:\t");
293 fprintf (stderr
, "The testcase must be run by GDB!\n");
296 if (tracer
!= getppid ())
298 fprintf (stderr
, "The testcase parent must be our GDB tracer!\n");
303 /* SIGCONT our debugger in the case of our crash as we would deadlock
308 printf ("Stopping GDB PID %lu.\n", (unsigned long) tracer
);
312 i
= kill (tracer
, SIGSTOP
);
314 state_wait (tracer
, "T (stopped)");
317 /* Threads are now waiting at timed_mutex_lock (thread1_tid_mutex) and so
318 they could not trigger the watchpoints before GDB gets unstopped later.
319 Threads get resumed at pthread_cond_wait below. Use `while' loops for
320 protection against spurious pthread_cond_wait wakeups. */
322 printf ("Waiting till the threads initialize their TIDs.\n");
324 while (thread1_tid
== 0)
326 i
= pthread_cond_wait (&thread1_tid_cond
, &thread1_tid_mutex
);
330 while (thread2_tid
== 0)
332 i
= pthread_cond_wait (&thread2_tid_cond
, &thread2_tid_mutex
);
336 printf ("Thread 1 TID = %lu, thread 2 TID = %lu, PID = %lu.\n",
337 (unsigned long) thread1_tid
, (unsigned long) thread2_tid
,
338 (unsigned long) getpid ());
340 printf ("Waiting till the threads get trapped by the watchpoints.\n");
344 /* s390x-unknown-linux-gnu will fail with "R (running)". */
346 state_wait (thread1_tid
, "t (tracing stop)");
348 state_wait (thread2_tid
, "t (tracing stop)");
353 printf ("Joining the threads.\n");
355 i
= pthread_mutex_unlock (&terminate_mutex
);
358 i
= pthread_join (thread1
, NULL
);
361 i
= pthread_join (thread2
, NULL
);
364 printf ("Exiting.\n"); /* break-at-exit */
366 /* Just prevent compiler `warning: unusedX_rwatch defined but not used'. */