gdb/
[external/binutils.git] / gdb / testsuite / gdb.threads / watchpoint-fork-child.c
1 /* Test case for forgotten hw-watchpoints after fork()-off of a process.
2
3    Copyright 2012 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 #include <string.h>
23 #include <errno.h>
24 #include <unistd.h>
25 #include <assert.h>
26 #include <signal.h>
27 #include <stdio.h>
28
29 #include "watchpoint-fork.h"
30
31 /* `pid_t' may not be available.  */
32
33 static volatile int usr1_got;
34
35 static void
36 handler_usr1 (int signo)
37 {
38   usr1_got++;
39 }
40
41 void
42 forkoff (int nr)
43 {
44   int child, save_parent = getpid ();
45   int i;
46   struct sigaction act, oldact;
47 #ifdef THREAD
48   void *thread_result;
49 #endif
50
51   memset (&act, 0, sizeof act);
52   act.sa_flags = SA_RESTART;
53   act.sa_handler = handler_usr1;
54   sigemptyset (&act.sa_mask);
55   i = sigaction (SIGUSR1, &act, &oldact);
56   assert (i == 0);
57
58   child = fork ();
59   switch (child)
60     {
61     case -1:
62       assert (0);
63     default:
64       printf ("parent%d: %d\n", nr, (int) child);
65
66       /* Sleep for a while to possibly get incorrectly ATTACH_THREADed by GDB
67          tracing the child fork with no longer valid thread/lwp entries of the
68          parent.  */
69
70       i = sleep (2);
71       assert (i == 0);
72
73       /* We must not get caught here (against a forgotten breakpoint).  */
74
75       var++;
76       marker ();
77
78 #ifdef THREAD
79       /* And neither got caught our thread.  */
80
81       step = 99;
82       i = pthread_join (thread, &thread_result);
83       assert (i == 0);
84       assert (thread_result == (void *) 99UL);
85 #endif
86
87       /* Be sure our child knows we did not get caught above.  */
88
89       i = kill (child, SIGUSR1);
90       assert (i == 0);
91
92       /* Sleep for a while to check GDB's `info threads' no longer tracks us in
93          the child fork.  */
94
95       i = sleep (2);
96       assert (i == 0);
97
98       _exit (0);
99     case 0:
100       printf ("child%d: %d\n", nr, (int) getpid ());
101
102       /* Let the parent signal us about its success.  Be careful of races.  */
103
104       for (;;)
105         {
106           /* Parent either died (and USR1_GOT is zero) or it succeeded.  */
107           if (getppid () != save_parent)
108             break;
109           if (kill (getppid (), 0) != 0)
110             break;
111           /* Parent succeeded?  */
112           if (usr1_got)
113             break;
114
115 #ifdef THREAD
116           i = pthread_yield ();
117           assert (i == 0);
118 #endif
119         }
120       assert (usr1_got);
121
122       /* We must get caught here (against a false watchpoint removal).  */
123
124       marker ();
125     }
126
127   i = sigaction (SIGUSR1, &oldact, NULL);
128   assert (i == 0);
129 }