[gdb/testsuite] Factor out lib/valgrind.exp
[external/binutils.git] / gdb / testsuite / gdb.base / longjmp.c
1 /* This testcase is part of GDB, the GNU debugger.
2
3    Copyright 2008-2018 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 #include <setjmp.h>
19
20 jmp_buf env;
21
22 volatile int longjmps = 0;
23 volatile int resumes = 0;
24
25 int
26 call_longjmp (jmp_buf *buf)
27 {
28   longjmps++;
29   longjmp (*buf, 1);
30 }
31
32 void
33 hidden_longjmp (void)
34 {
35   if (setjmp (env) == 0)
36     {
37       call_longjmp (&env);
38     }
39   else
40     resumes++;
41 }
42
43 int
44 main ()
45 {
46   volatile int i = 0;
47
48   /* Pattern 1 - simple longjmp.  */
49   if (setjmp (env) == 0) /* patt1 */
50     {
51       longjmps++;
52       longjmp (env, 1);
53     }
54   else
55     {
56       resumes++;
57     }
58
59   i = 1; /* miss_step_1 */
60
61
62   /* Pattern 2 - longjmp from an inner function.  */
63   if (setjmp (env) == 0) /* patt2 */
64     {
65       call_longjmp (&env);
66     }
67   else
68     {
69       resumes++;
70     }
71
72   i = 2; /* miss_step_2 */
73
74   /* Pattern 3 - setjmp/longjmp inside stepped-over function.  */
75   hidden_longjmp (); /* patt3 */
76
77   i = 77; /* longjmp caught */
78
79   i = 3; /* patt_end3.  */
80
81   return 0;
82 }