Commit | Line | Data |
---|---|---|
e5ef252a PA |
1 | /* This testcase is part of GDB, the GNU debugger. |
2 | ||
ecd75fc8 | 3 | Copyright 2010-2014 Free Software Foundation, Inc. |
e5ef252a PA |
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 | #define _GNU_SOURCE | |
19 | #include <pthread.h> | |
20 | #include <stdio.h> | |
21 | #include <limits.h> | |
22 | #include <errno.h> | |
23 | #include <stdlib.h> | |
24 | #include <string.h> | |
25 | #include <assert.h> | |
26 | #include <sys/types.h> | |
27 | #include <signal.h> | |
28 | #include <unistd.h> | |
29 | #include <asm/unistd.h> | |
30 | ||
31 | #define gettid() syscall (__NR_gettid) | |
32 | #define tgkill(tgid, tid, sig) syscall (__NR_tgkill, tgid, tid, sig) | |
33 | ||
34 | /* Terminate always in the main task. It can lock up with SIGSTOPped | |
35 | GDB otherwise. */ | |
36 | #define TIMEOUT (gettid () == getpid() ? 10 : 15) | |
37 | ||
38 | static pid_t thread1_tid; | |
39 | static pthread_cond_t thread1_tid_cond | |
40 | = PTHREAD_COND_INITIALIZER; | |
41 | static pthread_mutex_t thread1_tid_mutex | |
42 | = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP; | |
43 | static int thread1_sigusr1_hit; | |
44 | static int thread1_sigusr2_hit; | |
45 | ||
46 | static pid_t thread2_tid; | |
47 | static pthread_cond_t thread2_tid_cond | |
48 | = PTHREAD_COND_INITIALIZER; | |
49 | static pthread_mutex_t thread2_tid_mutex | |
50 | = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP; | |
51 | static int thread2_sigusr1_hit; | |
52 | static int thread2_sigusr2_hit; | |
53 | ||
54 | static pthread_mutex_t terminate_mutex | |
55 | = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP; | |
56 | ||
57 | /* Do not use alarm as it would create a ptrace event which would hang | |
58 | us up if we are being traced by GDB, which we stopped | |
59 | ourselves. */ | |
60 | ||
61 | static void | |
62 | timed_mutex_lock (pthread_mutex_t *mutex) | |
63 | { | |
64 | int i; | |
65 | struct timespec start, now; | |
66 | ||
67 | i = clock_gettime (CLOCK_MONOTONIC, &start); | |
68 | assert (i == 0); | |
69 | ||
70 | do | |
71 | { | |
72 | i = pthread_mutex_trylock (mutex); | |
73 | if (i == 0) | |
74 | return; | |
75 | assert (i == EBUSY); | |
76 | ||
77 | i = clock_gettime (CLOCK_MONOTONIC, &now); | |
78 | assert (i == 0); | |
79 | assert (now.tv_sec >= start.tv_sec); | |
80 | } | |
81 | while (now.tv_sec - start.tv_sec < TIMEOUT); | |
82 | ||
83 | fprintf (stderr, "Timed out waiting for internal lock!\n"); | |
84 | exit (EXIT_FAILURE); | |
85 | } | |
86 | ||
87 | static void | |
88 | handler (int signo, siginfo_t *siginfo, void *exception) | |
89 | { | |
90 | int *varp; | |
91 | ||
92 | assert (siginfo->si_signo == signo); | |
93 | assert (siginfo->si_code == SI_TKILL); | |
94 | assert (siginfo->si_pid == getpid ()); | |
95 | ||
96 | if (gettid () == thread1_tid) | |
97 | { | |
98 | if (signo == SIGUSR1) | |
99 | varp = &thread1_sigusr1_hit; | |
100 | else if (signo == SIGUSR2) | |
101 | varp = &thread1_sigusr2_hit; | |
102 | else | |
103 | assert (0); | |
104 | } | |
105 | else if (gettid () == thread2_tid) | |
106 | { | |
107 | if (signo == SIGUSR1) | |
108 | varp = &thread2_sigusr1_hit; | |
109 | else if (signo == SIGUSR2) | |
110 | varp = &thread2_sigusr2_hit; | |
111 | else | |
112 | assert (0); | |
113 | } | |
114 | else | |
115 | assert (0); | |
116 | ||
117 | if (*varp) | |
118 | { | |
119 | fprintf (stderr, "Signal %d for TID %lu has been already hit!\n", signo, | |
120 | (unsigned long) gettid ()); | |
121 | exit (EXIT_FAILURE); | |
122 | } | |
123 | *varp = 1; | |
124 | } | |
125 | ||
126 | static void * | |
127 | thread1_func (void *unused) | |
128 | { | |
129 | int i; | |
130 | ||
131 | timed_mutex_lock (&thread1_tid_mutex); | |
132 | ||
133 | /* THREAD1_TID_MUTEX must be already locked to avoid a race. */ | |
134 | thread1_tid = gettid (); | |
135 | ||
136 | i = pthread_cond_signal (&thread1_tid_cond); | |
137 | assert (i == 0); | |
138 | i = pthread_mutex_unlock (&thread1_tid_mutex); | |
139 | assert (i == 0); | |
140 | ||
141 | /* Be sure the "t (tracing stop)" test can proceed for both | |
142 | threads. */ | |
143 | timed_mutex_lock (&terminate_mutex); | |
144 | i = pthread_mutex_unlock (&terminate_mutex); | |
145 | assert (i == 0); | |
146 | ||
147 | if (!thread1_sigusr1_hit) | |
148 | { | |
149 | fprintf (stderr, "Thread 1 signal SIGUSR1 not hit!\n"); | |
150 | exit (EXIT_FAILURE); | |
151 | } | |
152 | if (!thread1_sigusr2_hit) | |
153 | { | |
154 | fprintf (stderr, "Thread 1 signal SIGUSR2 not hit!\n"); | |
155 | exit (EXIT_FAILURE); | |
156 | } | |
157 | ||
158 | return NULL; | |
159 | } | |
160 | ||
161 | static void * | |
162 | thread2_func (void *unused) | |
163 | { | |
164 | int i; | |
165 | ||
166 | timed_mutex_lock (&thread2_tid_mutex); | |
167 | ||
168 | /* THREAD2_TID_MUTEX must be already locked to avoid a race. */ | |
169 | thread2_tid = gettid (); | |
170 | ||
171 | i = pthread_cond_signal (&thread2_tid_cond); | |
172 | assert (i == 0); | |
173 | i = pthread_mutex_unlock (&thread2_tid_mutex); | |
174 | assert (i == 0); | |
175 | ||
176 | /* Be sure the "t (tracing stop)" test can proceed for both | |
177 | threads. */ | |
178 | timed_mutex_lock (&terminate_mutex); | |
179 | i = pthread_mutex_unlock (&terminate_mutex); | |
180 | assert (i == 0); | |
181 | ||
182 | if (!thread2_sigusr1_hit) | |
183 | { | |
184 | fprintf (stderr, "Thread 2 signal SIGUSR1 not hit!\n"); | |
185 | exit (EXIT_FAILURE); | |
186 | } | |
187 | if (!thread2_sigusr2_hit) | |
188 | { | |
189 | fprintf (stderr, "Thread 2 signal SIGUSR2 not hit!\n"); | |
190 | exit (EXIT_FAILURE); | |
191 | } | |
192 | ||
193 | return NULL; | |
194 | } | |
195 | ||
196 | static const char * | |
197 | proc_string (const char *filename, const char *line) | |
198 | { | |
199 | FILE *f; | |
200 | static char buf[LINE_MAX]; | |
201 | size_t line_len = strlen (line); | |
202 | ||
203 | f = fopen (filename, "r"); | |
204 | if (f == NULL) | |
205 | { | |
206 | fprintf (stderr, "fopen (\"%s\") for \"%s\": %s\n", filename, line, | |
207 | strerror (errno)); | |
208 | exit (EXIT_FAILURE); | |
209 | } | |
210 | while (errno = 0, fgets (buf, sizeof (buf), f)) | |
211 | { | |
212 | char *s; | |
213 | ||
214 | s = strchr (buf, '\n'); | |
215 | assert (s != NULL); | |
216 | *s = 0; | |
217 | ||
218 | if (strncmp (buf, line, line_len) != 0) | |
219 | continue; | |
220 | ||
221 | if (fclose (f)) | |
222 | { | |
223 | fprintf (stderr, "fclose (\"%s\") for \"%s\": %s\n", filename, line, | |
224 | strerror (errno)); | |
225 | exit (EXIT_FAILURE); | |
226 | } | |
227 | ||
228 | return &buf[line_len]; | |
229 | } | |
230 | if (errno != 0) | |
231 | { | |
232 | fprintf (stderr, "fgets (\"%s\": %s\n", filename, strerror (errno)); | |
233 | exit (EXIT_FAILURE); | |
234 | } | |
235 | fprintf (stderr, "\"%s\": No line \"%s\" found.\n", filename, line); | |
236 | exit (EXIT_FAILURE); | |
237 | } | |
238 | ||
239 | static unsigned long | |
240 | proc_ulong (const char *filename, const char *line) | |
241 | { | |
242 | const char *s = proc_string (filename, line); | |
243 | long retval; | |
244 | char *end; | |
245 | ||
246 | errno = 0; | |
247 | retval = strtol (s, &end, 10); | |
248 | if (retval < 0 || retval >= LONG_MAX || (end && *end)) | |
249 | { | |
250 | fprintf (stderr, "\"%s\":\"%s\": %ld, %s\n", filename, line, retval, | |
251 | strerror (errno)); | |
252 | exit (EXIT_FAILURE); | |
253 | } | |
254 | return retval; | |
255 | } | |
256 | ||
257 | static void | |
258 | state_wait (pid_t process, const char *wanted) | |
259 | { | |
260 | char *filename; | |
261 | int i; | |
262 | struct timespec start, now; | |
263 | const char *state; | |
264 | ||
265 | i = asprintf (&filename, "/proc/%lu/status", (unsigned long) process); | |
266 | assert (i > 0); | |
267 | ||
268 | i = clock_gettime (CLOCK_MONOTONIC, &start); | |
269 | assert (i == 0); | |
270 | ||
271 | do | |
272 | { | |
273 | state = proc_string (filename, "State:\t"); | |
274 | ||
275 | /* torvalds/linux-2.6.git 464763cf1c6df632dccc8f2f4c7e50163154a2c0 | |
276 | has changed "T (tracing stop)" to "t (tracing stop)". Make the GDB | |
277 | testcase backward compatible with older Linux kernels. */ | |
278 | if (strcmp (state, "T (tracing stop)") == 0) | |
279 | state = "t (tracing stop)"; | |
280 | ||
281 | if (strcmp (state, wanted) == 0) | |
282 | { | |
283 | free (filename); | |
284 | return; | |
285 | } | |
286 | ||
287 | if (sched_yield ()) | |
288 | { | |
289 | perror ("sched_yield()"); | |
290 | exit (EXIT_FAILURE); | |
291 | } | |
292 | ||
293 | i = clock_gettime (CLOCK_MONOTONIC, &now); | |
294 | assert (i == 0); | |
295 | assert (now.tv_sec >= start.tv_sec); | |
296 | } | |
297 | while (now.tv_sec - start.tv_sec < TIMEOUT); | |
298 | ||
299 | fprintf (stderr, "Timed out waiting for PID %lu \"%s\" (now it is \"%s\")!\n", | |
300 | (unsigned long) process, wanted, state); | |
301 | exit (EXIT_FAILURE); | |
302 | } | |
303 | ||
304 | static volatile pid_t tracer = 0; | |
305 | static pthread_t thread1, thread2; | |
306 | ||
307 | static void | |
308 | cleanup (void) | |
309 | { | |
310 | printf ("Resuming GDB PID %lu.\n", (unsigned long) tracer); | |
311 | ||
312 | if (tracer) | |
313 | { | |
314 | int i; | |
315 | int tracer_save = tracer; | |
316 | ||
317 | tracer = 0; | |
318 | ||
319 | i = kill (tracer_save, SIGCONT); | |
320 | assert (i == 0); | |
321 | } | |
322 | } | |
323 | ||
324 | int | |
325 | main (int argc, char **argv) | |
326 | { | |
327 | int i; | |
328 | int standalone = 0; | |
329 | struct sigaction act; | |
330 | ||
331 | if (argc == 2 && strcmp (argv[1], "-s") == 0) | |
332 | standalone = 1; | |
333 | else | |
334 | assert (argc == 1); | |
335 | ||
336 | setbuf (stdout, NULL); | |
337 | ||
338 | timed_mutex_lock (&thread1_tid_mutex); | |
339 | timed_mutex_lock (&thread2_tid_mutex); | |
340 | ||
341 | timed_mutex_lock (&terminate_mutex); | |
342 | ||
343 | errno = 0; | |
344 | memset (&act, 0, sizeof (act)); | |
345 | act.sa_sigaction = handler; | |
346 | act.sa_flags = SA_RESTART | SA_SIGINFO; | |
347 | i = sigemptyset (&act.sa_mask); | |
348 | assert_perror (errno); | |
349 | assert (i == 0); | |
350 | i = sigaction (SIGUSR1, &act, NULL); | |
351 | assert_perror (errno); | |
352 | assert (i == 0); | |
353 | i = sigaction (SIGUSR2, &act, NULL); | |
354 | assert_perror (errno); | |
355 | assert (i == 0); | |
356 | ||
357 | i = pthread_create (&thread1, NULL, thread1_func, NULL); | |
358 | assert (i == 0); | |
359 | ||
360 | i = pthread_create (&thread2, NULL, thread2_func, NULL); | |
361 | assert (i == 0); | |
362 | ||
363 | if (!standalone) | |
364 | { | |
365 | tracer = proc_ulong ("/proc/self/status", "TracerPid:\t"); | |
366 | if (tracer == 0) | |
367 | { | |
368 | fprintf (stderr, "The testcase must be run by GDB!\n"); | |
369 | exit (EXIT_FAILURE); | |
370 | } | |
371 | if (tracer != getppid ()) | |
372 | { | |
373 | fprintf (stderr, "The testcase parent must be our GDB tracer!\n"); | |
374 | exit (EXIT_FAILURE); | |
375 | } | |
376 | } | |
377 | ||
378 | /* SIGCONT our debugger in the case of our crash as we would deadlock | |
379 | otherwise. */ | |
380 | ||
381 | atexit (cleanup); | |
382 | ||
383 | printf ("Stopping GDB PID %lu.\n", (unsigned long) tracer); | |
384 | ||
385 | if (tracer) | |
386 | { | |
387 | i = kill (tracer, SIGSTOP); | |
388 | assert (i == 0); | |
389 | state_wait (tracer, "T (stopped)"); | |
390 | } | |
391 | ||
392 | /* Threads are now waiting at timed_mutex_lock (thread1_tid_mutex) | |
393 | and so they could not trigger the signals before GDB is unstopped | |
394 | later. Threads get resumed by the pthread_cond_wait below. Use | |
395 | `while' loops for protection against spurious pthread_cond_wait | |
396 | wakeups. */ | |
397 | ||
398 | printf ("Waiting till the threads initialize their TIDs.\n"); | |
399 | ||
400 | while (thread1_tid == 0) | |
401 | { | |
402 | i = pthread_cond_wait (&thread1_tid_cond, &thread1_tid_mutex); | |
403 | assert (i == 0); | |
404 | } | |
405 | ||
406 | while (thread2_tid == 0) | |
407 | { | |
408 | i = pthread_cond_wait (&thread2_tid_cond, &thread2_tid_mutex); | |
409 | assert (i == 0); | |
410 | } | |
411 | ||
412 | printf ("Thread 1 TID = %lu, thread 2 TID = %lu, PID = %lu.\n", | |
413 | (unsigned long) thread1_tid, (unsigned long) thread2_tid, | |
414 | (unsigned long) getpid ()); | |
415 | ||
416 | errno = 0; | |
417 | i = tgkill (getpid (), thread1_tid, SIGUSR1); | |
418 | assert_perror (errno); | |
419 | assert (i == 0); | |
420 | i = tgkill (getpid (), thread1_tid, SIGUSR2); | |
421 | assert_perror (errno); | |
422 | assert (i == 0); | |
423 | i = tgkill (getpid (), thread2_tid, SIGUSR1); | |
424 | assert_perror (errno); | |
425 | assert (i == 0); | |
426 | i = tgkill (getpid (), thread2_tid, SIGUSR2); | |
427 | assert_perror (errno); | |
428 | assert (i == 0); | |
429 | ||
430 | printf ("Waiting till the threads are trapped by the signals.\n"); | |
431 | ||
432 | if (tracer) | |
433 | { | |
434 | /* s390x-unknown-linux-gnu will fail with "R (running)". */ | |
435 | ||
436 | state_wait (thread1_tid, "t (tracing stop)"); | |
437 | ||
438 | state_wait (thread2_tid, "t (tracing stop)"); | |
439 | } | |
440 | ||
441 | cleanup (); | |
442 | ||
443 | printf ("Joining the threads.\n"); | |
444 | ||
445 | i = pthread_mutex_unlock (&terminate_mutex); | |
446 | assert (i == 0); | |
447 | ||
448 | i = pthread_join (thread1, NULL); | |
449 | assert (i == 0); | |
450 | ||
451 | i = pthread_join (thread2, NULL); | |
452 | assert (i == 0); | |
453 | ||
454 | printf ("Exiting.\n"); /* break-at-exit */ | |
455 | ||
456 | return EXIT_SUCCESS; | |
457 | } |