]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.threads/pending-fork-event-detach.c
gdb, gdbserver: detach fork child when detaching from fork parent
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.threads / pending-fork-event-detach.c
CommitLineData
7b961964
SM
1/* This testcase is part of GDB, the GNU debugger.
2
3 Copyright 2021 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#include <pthread.h>
19#include <unistd.h>
20#include <assert.h>
21
22static volatile int release_forking_thread = 0;
23static int x;
24static pthread_barrier_t barrier;
25
26static void
27break_here (void)
28{
29 x++;
30}
31
32static void
33do_fork (void)
34{
7b961964
SM
35 while (!release_forking_thread);
36
37 if (FORK_FUNCTION () == 0)
df5ad102
SM
38 {
39 /* We create the file in a separate program that we exec: if FORK_FUNCTION
40 is vfork, we shouldn't do anything more than an exec. */
41 execl (TOUCH_FILE_BIN, TOUCH_FILE_BIN, NULL);
42 }
7b961964
SM
43
44}
45
46static void *
47thread_func (void *p)
48{
df5ad102
SM
49 pthread_barrier_wait (&barrier);
50
7b961964
SM
51#if defined(MAIN_THREAD_FORKS)
52 break_here ();
53#elif defined(OTHER_THREAD_FORKS)
54 do_fork ();
55#else
56# error "MAIN_THREAD_FORKS or OTHER_THREAD_FORKS must be defined"
57#endif
58}
59
60
61int
62main (void)
63{
64 pthread_t thread;
65 int ret;
66
67 pthread_barrier_init (&barrier, NULL, 2);
68
69 alarm (30);
70 ret = pthread_create (&thread, NULL, thread_func, NULL);
71 assert (ret == 0);
72
73 pthread_barrier_wait (&barrier);
74
75#if defined(MAIN_THREAD_FORKS)
76 do_fork ();
77#elif defined(OTHER_THREAD_FORKS)
78 break_here ();
79#else
80# error "MAIN_THREAD_FORKS or OTHER_THREAD_FORKS must be defined"
81#endif
82
83 pthread_join (thread, NULL);
84
85 return 0;
86}