Remove obsolete test from gdb.cp/var-tag.exp
[binutils-gdb.git] / gdb / testsuite / gdb.threads / detach-step-over.c
blob17fcd5053d6d5496dc6bccabe22aa458862fc20c
1 /* This testcase is part of GDB, the GNU debugger.
3 Copyright 2021-2024 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/>. */
18 #define _GNU_SOURCE
19 #include <assert.h>
20 #include <pthread.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include <signal.h>
27 /* Number of threads we'll create. */
28 int n_threads = 10;
30 int mypid;
32 static void
33 setup_done (void)
37 /* Entry point for threads. Loops forever. */
39 void *
40 thread_func (void *arg)
42 /* Avoid setting the breakpoint at an instruction that wouldn't
43 require a fixup phase, like a branch/jump. In such a case, even
44 if GDB manages to detach the inferior with an incomplete
45 displaced step, GDB inferior may still not crash. A breakpoint
46 at a line that increments a variable is good bet that we end up
47 setting a breakpoint at an instruction that will require a fixup
48 phase to move the PC from the scratch pad to the instruction
49 after the breakpoint. */
50 volatile unsigned counter = 0;
52 while (1)
54 counter++; /* Set breakpoint here. */
55 counter++;
56 counter++;
59 return NULL;
62 /* Allow for as much timeout as DejaGnu wants, plus a bit of
63 slack. */
64 #define SECONDS (TIMEOUT + 20)
66 /* We'll exit after this many seconds. */
67 unsigned int seconds_left = SECONDS;
69 /* GDB sets this whenever it's about to start a new detach/attach
70 sequence. We react by resetting the seconds-left counter. */
71 volatile int again = 0;
73 int
74 main (int argc, char **argv)
76 int i;
78 signal (SIGUSR1, SIG_IGN);
80 mypid = getpid ();
81 setup_done ();
83 if (argc > 1)
84 n_threads = atoi (argv[1]);
86 /* Spawn the test threads. */
87 for (i = 0; i < n_threads; ++i)
89 pthread_t child;
90 int rc;
92 rc = pthread_create (&child, NULL, thread_func, NULL);
93 assert (rc == 0);
96 /* Exit after a while if GDB is gone/crashes. But wait long enough
97 for one attach/detach sequence done by the .exp file. */
98 while (--seconds_left > 0)
100 sleep (1);
102 if (again)
104 /* GDB should be reattaching soon. Restart the timer. */
105 again = 0;
106 seconds_left = SECONDS;
110 printf ("timeout, exiting\n");
111 return 0;