]> Git Repo - binutils.git/blob - gdb/testsuite/gdb.threads/vfork-multi-thread.c
Automatic date update in version.in
[binutils.git] / gdb / testsuite / gdb.threads / vfork-multi-thread.c
1 /* This testcase is part of GDB, the GNU debugger.
2
3    Copyright 2022 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 <assert.h>
19 #include <pthread.h>
20 #include <unistd.h>
21 #include <sys/wait.h>
22
23 static volatile int release_vfork = 0;
24 static volatile int release_main = 0;
25
26 static void *
27 vforker (void *arg)
28 {
29   while (!release_vfork)
30     usleep (1);
31
32   pid_t pid = vfork ();
33   if (pid == 0)
34     {
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.  */
37       release_main = 1;
38       _exit(7);
39     }
40
41   int stat;
42   int ret = waitpid (pid, &stat, 0);
43   assert (ret == pid);
44   assert (WIFEXITED (stat));
45   assert (WEXITSTATUS (stat) == 7);
46
47   return NULL;
48 }
49
50 static void
51 should_break_here (void)
52 {}
53
54 int
55 main (void)
56 {
57
58   pthread_t thread;
59   int ret = pthread_create (&thread, NULL, vforker, NULL);
60   assert (ret == 0);
61
62   /* We break here first, while the thread is stuck on `!release_fork`.  */
63   release_vfork = 1;
64
65   /* We set a breakpoint on should_break_here.
66
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.  */
79
80   while (!release_main)
81     usleep (1);
82
83   should_break_here ();
84
85   pthread_join (thread, NULL);
86
87   return 6;
88 }
This page took 0.028935 seconds and 4 git commands to generate.