1/* This testcase is part of GDB, the GNU debugger. 2 3 Copyright 2021-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 <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> 26 27/* Number of threads we'll create. */ 28int n_threads = 10; 29 30int mypid; 31 32static void 33setup_done (void) 34{ 35} 36 37/* Entry point for threads. Loops forever. */ 38 39void * 40thread_func (void *arg) 41{ 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; 51 52 while (1) 53 { 54 counter++; /* Set breakpoint here. */ 55 counter++; 56 counter++; 57 } 58 59 return NULL; 60} 61 62/* Allow for as much timeout as DejaGnu wants, plus a bit of 63 slack. */ 64#define SECONDS (TIMEOUT + 20) 65 66/* We'll exit after this many seconds. */ 67unsigned int seconds_left = SECONDS; 68 69/* GDB sets this whenever it's about to start a new detach/attach 70 sequence. We react by resetting the seconds-left counter. */ 71volatile int again = 0; 72 73int 74main (int argc, char **argv) 75{ 76 int i; 77 78 signal (SIGUSR1, SIG_IGN); 79 80 mypid = getpid (); 81 setup_done (); 82 83 if (argc > 1) 84 n_threads = atoi (argv[1]); 85 86 /* Spawn the test threads. */ 87 for (i = 0; i < n_threads; ++i) 88 { 89 pthread_t child; 90 int rc; 91 92 rc = pthread_create (&child, NULL, thread_func, NULL); 93 assert (rc == 0); 94 } 95 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) 99 { 100 sleep (1); 101 102 if (again) 103 { 104 /* GDB should be reattaching soon. Restart the timer. */ 105 again = 0; 106 seconds_left = SECONDS; 107 } 108 } 109 110 printf ("timeout, exiting\n"); 111 return 0; 112} 113