binutils/
[external/binutils.git] / gdb / testsuite / gdb.reverse / until-reverse.c
1 /* This testcase is part of GDB, the GNU debugger.
2
3    Copyright 2008, 2009, 2010, 2011 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 #ifdef vxworks
19
20 #  include <stdio.h>
21
22 /* VxWorks does not supply atoi.  */
23 static int
24 atoi (z)
25      char *z;
26 {
27   int i = 0;
28
29   while (*z >= '0' && *z <= '9')
30     i = i * 10 + (*z++ - '0');
31   return i;
32 }
33
34 /* I don't know of any way to pass an array to VxWorks.  This function
35    can be called directly from gdb.  */
36
37 vxmain (arg)
38 char *arg;
39 {
40   char *argv[2];
41
42   argv[0] = "";
43   argv[1] = arg;
44   main (2, argv, (char **) 0);
45 }
46
47 #else /* ! vxworks */
48 #  include <stdio.h>
49 #  include <stdlib.h>
50 #endif /* ! vxworks */
51
52 #ifdef PROTOTYPES
53 extern int marker1 (void);
54 extern int marker2 (int a);
55 extern void marker3 (char *a, char *b);
56 extern void marker4 (long d);
57 #else
58 extern int marker1 ();
59 extern int marker2 ();
60 extern void marker3 ();
61 extern void marker4 ();
62 #endif
63
64 /*
65  *      This simple classical example of recursion is useful for
66  *      testing stack backtraces and such.
67  */
68
69 #ifdef PROTOTYPES
70 int factorial(int);
71
72 int
73 main (int argc, char **argv, char **envp)
74 #else
75 int
76 main (argc, argv, envp)
77 int argc;
78 char *argv[], **envp;
79 #endif
80 {
81 #ifdef usestubs
82     set_debug_traps();  /* set breakpoint 5 here */
83     breakpoint();
84 #endif
85     if (argc == 12345) {  /* an unlikely value < 2^16, in case uninited */ /* set breakpoint 6 here */
86         fprintf (stderr, "usage:  factorial <number>\n");
87         return 1;
88     }
89     printf ("%d\n", factorial (atoi ("6")));  /* set breakpoint 1 here */
90     /* set breakpoint 12 here */
91     marker1 ();  /* set breakpoint 11 here */
92     marker2 (43); /* set breakpoint 20 here */
93     marker3 ("stack", "trace"); /* set breakpoint 21 here */
94     marker4 (177601976L);
95     /* We're used by a test that requires malloc, so make sure it is
96        in the executable.  */
97     (void)malloc (1);
98
99     argc = (argc == 12345); /* This is silly, but we can step off of it */ /* set breakpoint 2 here */
100     return argc;  /* set breakpoint 10 here */
101 } /* set breakpoint 10a here */
102
103 #ifdef PROTOTYPES
104 int factorial (int value)
105 #else
106 int factorial (value)
107 int value;
108 #endif
109 {
110   if (value > 1) {  /* set breakpoint 7 here */
111         value *= factorial (value - 1);
112     }
113     return (value); /* set breakpoint 19 here */
114 }
115
116 #ifdef PROTOTYPES
117 int multi_line_if_conditional (int a, int b, int c)
118 #else
119 int multi_line_if_conditional (a, b, c)
120   int a, b, c;
121 #endif
122 {
123   if (a    /* set breakpoint 3 here */
124       && b
125       && c)
126     return 0;
127   else
128     return 1;
129 }
130
131 #ifdef PROTOTYPES
132 int multi_line_while_conditional (int a, int b, int c)
133 #else
134 int multi_line_while_conditional (a, b, c)
135   int a, b, c;
136 #endif
137 {
138   while (a /* set breakpoint 4 here */
139       && b
140       && c)
141     {
142       a--, b--, c--;
143     }
144   return 0;
145 }