summaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.threads/forking-threads-plus-breakpoint.c
blob: e7d4617674bcf9ac35c713b2f7fcdf3c805d1093 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* This testcase is part of GDB, the GNU debugger.

   Copyright 2015-2017 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

#include <assert.h>
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <errno.h>

/* Number of threads.  Each thread continuously spawns a fork and wait
   for it.  If we have another thread continuously start a step over,
   gdbserver should end up finding new forks while suspending
   threads.  */
#define NTHREADS 10

pthread_t threads[NTHREADS];

pthread_barrier_t barrier;

#define NFORKS 10

/* Used to create a conditional breakpoint that always fails.  */
volatile int zero;

static void *
thread_forks (void *arg)
{
  int i;

  pthread_barrier_wait (&barrier);

  for (i = 0; i < NFORKS; i++)
    {
      pid_t pid;

      do
	{
	  pid = fork ();
	}
      while (pid == -1 && errno == EINTR);

      if (pid > 0)
	{
	  int status;

	  /* Parent.  */
	  do
	    {
	      pid = waitpid (pid, &status, 0);
	    }
	  while (pid == -1 && errno == EINTR);

	  if (pid == -1)
	    {
	      perror ("wait");
	      exit (1);
	    }

	  if (!WIFEXITED (status))
	    {
	      printf ("Unexpected wait status 0x%x from child %d\n",
		      status, pid);
	    }
	}
      else if (pid == 0)
	{
	  /* Child.  */
	  exit (0);
	}
      else
	{
	  perror ("fork");
	  exit (1);
	}
    }
}

/* Set this to tell the thread_breakpoint thread to exit.  */
volatile int break_out;

static void *
thread_breakpoint (void *arg)
{
  pthread_barrier_wait (&barrier);

  while (!break_out)
    {
      usleep (1); /* set break here */
    }

  return NULL;
}

pthread_barrier_t barrier;

int
main (void)
{
  int i;
  int ret;
  pthread_t bp_thread;

  /* Don't run forever.  */
  alarm (180);

  pthread_barrier_init (&barrier, NULL, NTHREADS + 1);

  /* Start the threads that constantly fork.  */
  for (i = 0; i < NTHREADS; i++)
    {
      ret = pthread_create (&threads[i], NULL, thread_forks, NULL);
      assert (ret == 0);
    }

  /* Start the thread that constantly hit a conditional breakpoint
     that needs to be stepped over.  */
  ret = pthread_create (&bp_thread, NULL, thread_breakpoint, NULL);
  assert (ret == 0);

  /* Wait for forking to stop.  */
  for (i = 0; i < NTHREADS; i++)
    {
      ret = pthread_join (threads[i], NULL);
      assert (ret == 0);
    }

  break_out = 1;
  pthread_join (bp_thread, NULL);
  assert (ret == 0);

  return 0;
}