Home | History | Annotate | Line # | Download | only in gdb.base
step-over-clone.c revision 1.1.1.5
      1 /* This testcase is part of GDB, the GNU debugger.
      2 
      3    Copyright 2016-2023 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 #define _GNU_SOURCE
     19 #include <stdlib.h>
     20 #include <unistd.h>
     21 #include <sched.h>
     22 #include <pthread.h>
     23 
     24 static void
     25 marker ()
     26 {}
     27 
     28 #define STACK_SIZE 0x1000
     29 
     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.  */
     33 pthread_mutex_t global_lock = PTHREAD_MUTEX_INITIALIZER;
     34 int global_thread_count = 0;
     35 
     36 static int
     37 clone_fn (void *unused)
     38 {
     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 
     46   return 0;
     47 }
     48 
     49 int
     50 main (void)
     51 {
     52   int i, pid;
     53   unsigned char *stack[6];
     54 
     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 
     65   for (i = 0; i < (sizeof (stack) / sizeof (stack[0])); i++)
     66     stack[i] = malloc (STACK_SIZE);
     67 
     68   global_thread_count = (sizeof (stack) / sizeof (stack[0]));
     69 
     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 
     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.  */
     92   marker ();
     93 }
     94