1 /* This testcase is part of GDB, the GNU debugger.
3 Copyright 2022 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/>. */
23 static volatile int release_vfork
= 0;
24 static volatile int release_main
= 0;
29 while (!release_vfork
)
35 /* A vfork child is not supposed to mess with the state of the program,
36 but it is helpful for the purpose of this test. */
42 int ret
= waitpid (pid
, &stat
, 0);
44 assert (WIFEXITED (stat
));
45 assert (WEXITSTATUS (stat
) == 7);
51 should_break_here (void)
59 int ret
= pthread_create (&thread
, NULL
, vforker
, NULL
);
62 /* We break here first, while the thread is stuck on `!release_fork`. */
65 /* We set a breakpoint on should_break_here.
67 We then set "release_fork" from the debugger and continue. The main
68 thread hangs on `!release_main` while the non-main thread vforks. During
69 the window of time where the two processes have a shared address space
70 (after vfork, before _exit), GDB removes the breakpoints from the address
71 space. During that window, only the vfork-ing thread (the non-main
72 thread) is frozen by the kernel. The main thread is free to execute. The
73 child process sets `release_main`, releasing the main thread. A buggy GDB
74 would let the main thread execute during that window, leading to the
75 breakpoint on should_break_here being missed. A fixed GDB does not resume
76 the threads of the vforking process other than the vforking thread. When
77 the vfork child exits, the fixed GDB resumes the main thread, after
78 breakpoints are reinserted, so the breakpoint is not missed. */
85 pthread_join (thread
, NULL
);