]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.base/step-over-clone.c
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / step-over-clone.c
CommitLineData
4719d415
YQ
1/* This testcase is part of GDB, the GNU debugger.
2
213516ef 3 Copyright 2016-2023 Free Software Foundation, Inc.
4719d415
YQ
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 <stdlib.h>
20#include <unistd.h>
21#include <sched.h>
99ba4b64 22#include <pthread.h>
4719d415
YQ
23
24static void
25marker ()
26{}
27
28#define STACK_SIZE 0x1000
29
99ba4b64
AB
30/* These are used to signal that the threads have started correctly. The
31 GLOBAL_THREAD_COUNT is set to the number of threads in main, then
32 decremented (under a lock) in each new thread. */
33pthread_mutex_t global_lock = PTHREAD_MUTEX_INITIALIZER;
34int global_thread_count = 0;
35
4719d415
YQ
36static int
37clone_fn (void *unused)
38{
99ba4b64
AB
39 /* Signal that this thread has started correctly. */
40 if (pthread_mutex_lock (&global_lock) != 0)
41 abort ();
42 global_thread_count--;
43 if (pthread_mutex_unlock (&global_lock) != 0)
44 abort ();
45
4719d415
YQ
46 return 0;
47}
48
49int
50main (void)
51{
52 int i, pid;
53 unsigned char *stack[6];
54
99ba4b64
AB
55 /* Due to bug gdb/19675 the cloned thread _might_ try to reenter main
56 (this depends on where the displaced instruction is placed for
57 execution). However, if we do reenter main then lets ensure we fail
58 hard rather then just silently executing the code below. */
59 static int started = 0;
60 if (!started)
61 started = 1;
62 else
63 abort ();
64
4719d415
YQ
65 for (i = 0; i < (sizeof (stack) / sizeof (stack[0])); i++)
66 stack[i] = malloc (STACK_SIZE);
67
99ba4b64
AB
68 global_thread_count = (sizeof (stack) / sizeof (stack[0]));
69
4719d415
YQ
70 for (i = 0; i < (sizeof (stack) / sizeof (stack[0])); i++)
71 {
72 pid = clone (clone_fn, stack[i] + STACK_SIZE, CLONE_FILES | CLONE_VM,
73 NULL);
74 }
75
76 for (i = 0; i < (sizeof (stack) / sizeof (stack[0])); i++)
77 free (stack[i]);
78
99ba4b64
AB
79 /* Set an alarm so we don't end up stuck waiting for threads that might
80 never start correctly. */
81 alarm (120);
82
83 /* Now wait for all the threads to start up. */
84 while (global_thread_count != 0)
85 {
86 /* Force memory barrier so GLOBAL_THREAD_COUNT will be refetched. */
87 asm volatile ("" ::: "memory");
88 sleep (1);
89 }
90
91 /* Call marker, this is what GDB is waiting for. */
4719d415
YQ
92 marker ();
93}