1*b725ae77Skettenis /* This testcase is part of GDB, the GNU debugger.
2*b725ae77Skettenis
3*b725ae77Skettenis Copyright 1993, 1994, 1995, 1998, 1999, 2000, 2001, 2004
4*b725ae77Skettenis Free Software Foundation, Inc.
5*b725ae77Skettenis
6*b725ae77Skettenis This program is free software; you can redistribute it and/or modify
7*b725ae77Skettenis it under the terms of the GNU General Public License as published by
8*b725ae77Skettenis the Free Software Foundation; either version 2 of the License, or
9*b725ae77Skettenis (at your option) any later version.
10*b725ae77Skettenis
11*b725ae77Skettenis This program is distributed in the hope that it will be useful,
12*b725ae77Skettenis but WITHOUT ANY WARRANTY; without even the implied warranty of
13*b725ae77Skettenis MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*b725ae77Skettenis GNU General Public License for more details.
15*b725ae77Skettenis
16*b725ae77Skettenis You should have received a copy of the GNU General Public License
17*b725ae77Skettenis along with this program; if not, write to the Free Software
18*b725ae77Skettenis Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19*b725ae77Skettenis
20*b725ae77Skettenis Please email any bugs, comments, and/or additions to this file to:
21*b725ae77Skettenis bug-gdb@prep.ai.mit.edu */
22*b725ae77Skettenis
23e93f7393Sniklas /* Support program for testing gdb's ability to call functions
24e93f7393Sniklas in the inferior, pass appropriate arguments to those functions,
25e93f7393Sniklas and get the returned result. */
26e93f7393Sniklas
27e93f7393Sniklas #ifdef NO_PROTOTYPES
28e93f7393Sniklas #define PARAMS(paramlist) ()
29e93f7393Sniklas #else
30e93f7393Sniklas #define PARAMS(paramlist) paramlist
31e93f7393Sniklas #endif
32e93f7393Sniklas
33*b725ae77Skettenis # include <stdlib.h>
34*b725ae77Skettenis # include <string.h>
35*b725ae77Skettenis
36e93f7393Sniklas char char_val1 = 'a';
37e93f7393Sniklas char char_val2 = 'b';
38e93f7393Sniklas
39e93f7393Sniklas short short_val1 = 10;
40e93f7393Sniklas short short_val2 = -23;
41e93f7393Sniklas
42e93f7393Sniklas int int_val1 = 87;
43e93f7393Sniklas int int_val2 = -26;
44e93f7393Sniklas
45e93f7393Sniklas long long_val1 = 789;
46e93f7393Sniklas long long_val2 = -321;
47e93f7393Sniklas
48e93f7393Sniklas float float_val1 = 3.14159;
49e93f7393Sniklas float float_val2 = -2.3765;
50e93f7393Sniklas
51e93f7393Sniklas double double_val1 = 45.654;
52e93f7393Sniklas double double_val2 = -67.66;
53e93f7393Sniklas
54e93f7393Sniklas #define DELTA (0.001)
55e93f7393Sniklas
56*b725ae77Skettenis char *string_val1 = (char *)"string 1";
57*b725ae77Skettenis char *string_val2 = (char *)"string 2";
58e93f7393Sniklas
59e93f7393Sniklas char char_array_val1[] = "carray 1";
60e93f7393Sniklas char char_array_val2[] = "carray 2";
61e93f7393Sniklas
62e93f7393Sniklas struct struct1 {
63e93f7393Sniklas char c;
64e93f7393Sniklas short s;
65e93f7393Sniklas int i;
66e93f7393Sniklas long l;
67e93f7393Sniklas float f;
68e93f7393Sniklas double d;
69e93f7393Sniklas char a[4];
70e93f7393Sniklas } struct_val1 = { 'x', 87, 76, 51, 2.1234, 9.876, "foo" };
71e93f7393Sniklas
72e93f7393Sniklas /* Some functions that can be passed as arguments to other test
73e93f7393Sniklas functions, or called directly. */
74*b725ae77Skettenis #ifdef PROTOTYPES
add(int a,int b)75*b725ae77Skettenis int add (int a, int b)
76*b725ae77Skettenis #else
77*b725ae77Skettenis int add (a, b) int a, b;
78*b725ae77Skettenis #endif
79e93f7393Sniklas {
80e93f7393Sniklas return (a + b);
81e93f7393Sniklas }
82e93f7393Sniklas
83*b725ae77Skettenis #ifdef PROTOTYPES
doubleit(int a)84*b725ae77Skettenis int doubleit (int a)
85*b725ae77Skettenis #else
86*b725ae77Skettenis int doubleit (a) int a;
87*b725ae77Skettenis #endif
88e93f7393Sniklas {
89e93f7393Sniklas return (a + a);
90e93f7393Sniklas }
91e93f7393Sniklas
92e93f7393Sniklas int (*func_val1) PARAMS((int,int)) = add;
93e93f7393Sniklas int (*func_val2) PARAMS((int)) = doubleit;
94e93f7393Sniklas
95e93f7393Sniklas /* An enumeration and functions that test for specific values. */
96e93f7393Sniklas
97e93f7393Sniklas enum enumtype { enumval1, enumval2, enumval3 };
98e93f7393Sniklas enum enumtype enum_val1 = enumval1;
99e93f7393Sniklas enum enumtype enum_val2 = enumval2;
100e93f7393Sniklas enum enumtype enum_val3 = enumval3;
101e93f7393Sniklas
102*b725ae77Skettenis #ifdef PROTOTYPES
t_enum_value1(enum enumtype enum_arg)103*b725ae77Skettenis int t_enum_value1 (enum enumtype enum_arg)
104*b725ae77Skettenis #else
105*b725ae77Skettenis int t_enum_value1 (enum_arg) enum enumtype enum_arg;
106*b725ae77Skettenis #endif
107e93f7393Sniklas {
108e93f7393Sniklas return (enum_arg == enum_val1);
109e93f7393Sniklas }
110e93f7393Sniklas
111*b725ae77Skettenis #ifdef PROTOTYPES
t_enum_value2(enum enumtype enum_arg)112*b725ae77Skettenis int t_enum_value2 (enum enumtype enum_arg)
113*b725ae77Skettenis #else
114*b725ae77Skettenis int t_enum_value2 (enum_arg) enum enumtype enum_arg;
115*b725ae77Skettenis #endif
116e93f7393Sniklas {
117e93f7393Sniklas return (enum_arg == enum_val2);
118e93f7393Sniklas }
119e93f7393Sniklas
120*b725ae77Skettenis #ifdef PROTOTYPES
t_enum_value3(enum enumtype enum_arg)121*b725ae77Skettenis int t_enum_value3 (enum enumtype enum_arg)
122*b725ae77Skettenis #else
123*b725ae77Skettenis int t_enum_value3 (enum_arg) enum enumtype enum_arg;
124*b725ae77Skettenis #endif
125e93f7393Sniklas {
126e93f7393Sniklas return (enum_arg == enum_val3);
127e93f7393Sniklas }
128e93f7393Sniklas
129e93f7393Sniklas /* A function that takes a vector of integers (along with an explicit
130e93f7393Sniklas count) and returns their sum. */
131e93f7393Sniklas
132*b725ae77Skettenis #ifdef PROTOTYPES
sum_args(int argc,int argv[])133*b725ae77Skettenis int sum_args (int argc, int argv[])
134*b725ae77Skettenis #else
135*b725ae77Skettenis int sum_args (argc, argv) int argc; int argv[];
136*b725ae77Skettenis #endif
137e93f7393Sniklas {
138e93f7393Sniklas int sumval = 0;
139e93f7393Sniklas int idx;
140e93f7393Sniklas
141e93f7393Sniklas for (idx = 0; idx < argc; idx++)
142e93f7393Sniklas {
143e93f7393Sniklas sumval += argv[idx];
144e93f7393Sniklas }
145e93f7393Sniklas return (sumval);
146e93f7393Sniklas }
147e93f7393Sniklas
148e93f7393Sniklas /* Test that we can call functions that take structs and return
149e93f7393Sniklas members from that struct */
150e93f7393Sniklas
151*b725ae77Skettenis #ifdef PROTOTYPES
t_structs_c(struct struct1 tstruct)152*b725ae77Skettenis char t_structs_c (struct struct1 tstruct) { return (tstruct.c); }
t_structs_s(struct struct1 tstruct)153*b725ae77Skettenis short t_structs_s (struct struct1 tstruct) { return (tstruct.s); }
t_structs_i(struct struct1 tstruct)154*b725ae77Skettenis int t_structs_i (struct struct1 tstruct) { return (tstruct.i); }
t_structs_l(struct struct1 tstruct)155*b725ae77Skettenis long t_structs_l (struct struct1 tstruct) { return (tstruct.l); }
t_structs_f(struct struct1 tstruct)156*b725ae77Skettenis float t_structs_f (struct struct1 tstruct) { return (tstruct.f); }
t_structs_d(struct struct1 tstruct)157*b725ae77Skettenis double t_structs_d (struct struct1 tstruct) { return (tstruct.d); }
t_structs_a(struct struct1 tstruct)158*b725ae77Skettenis char *t_structs_a (struct struct1 tstruct)
159*b725ae77Skettenis {
160*b725ae77Skettenis static char buf[8];
161*b725ae77Skettenis strcpy (buf, tstruct.a);
162*b725ae77Skettenis return buf;
163*b725ae77Skettenis }
164*b725ae77Skettenis #else
t_structs_c(tstruct)165e93f7393Sniklas char t_structs_c (tstruct) struct struct1 tstruct; { return (tstruct.c); }
t_structs_s(tstruct)166e93f7393Sniklas short t_structs_s (tstruct) struct struct1 tstruct; { return (tstruct.s); }
t_structs_i(tstruct)167e93f7393Sniklas int t_structs_i (tstruct) struct struct1 tstruct; { return (tstruct.i); }
t_structs_l(tstruct)168e93f7393Sniklas long t_structs_l (tstruct) struct struct1 tstruct; { return (tstruct.l); }
t_structs_f(tstruct)169e93f7393Sniklas float t_structs_f (tstruct) struct struct1 tstruct; { return (tstruct.f); }
t_structs_d(tstruct)170e93f7393Sniklas double t_structs_d (tstruct) struct struct1 tstruct; { return (tstruct.d); }
t_structs_a(tstruct)171*b725ae77Skettenis char *t_structs_a (tstruct) struct struct1 tstruct;
172*b725ae77Skettenis {
173*b725ae77Skettenis static char buf[8];
174*b725ae77Skettenis strcpy (buf, tstruct.a);
175*b725ae77Skettenis return buf;
176*b725ae77Skettenis }
177*b725ae77Skettenis #endif
178e93f7393Sniklas
179e93f7393Sniklas /* Test that calling functions works if there are a lot of arguments. */
180*b725ae77Skettenis #ifdef PROTOTYPES
181*b725ae77Skettenis int
sum10(int i0,int i1,int i2,int i3,int i4,int i5,int i6,int i7,int i8,int i9)182*b725ae77Skettenis sum10 (int i0, int i1, int i2, int i3, int i4, int i5, int i6, int i7, int i8, int i9)
183*b725ae77Skettenis #else
184e93f7393Sniklas int
185e93f7393Sniklas sum10 (i0, i1, i2, i3, i4, i5, i6, i7, i8, i9)
186e93f7393Sniklas int i0, i1, i2, i3, i4, i5, i6, i7, i8, i9;
187*b725ae77Skettenis #endif
188e93f7393Sniklas {
189e93f7393Sniklas return i0 + i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8 + i9;
190e93f7393Sniklas }
191e93f7393Sniklas
192*b725ae77Skettenis /* Test that args are passed in the right order. */
193*b725ae77Skettenis #ifdef PROTOTYPES
194*b725ae77Skettenis int
cmp10(int i0,int i1,int i2,int i3,int i4,int i5,int i6,int i7,int i8,int i9)195*b725ae77Skettenis cmp10 (int i0, int i1, int i2, int i3, int i4, int i5, int i6, int i7, int i8, int i9)
196*b725ae77Skettenis #else
197*b725ae77Skettenis int
198*b725ae77Skettenis cmp10 (i0, i1, i2, i3, i4, i5, i6, i7, i8, i9)
199*b725ae77Skettenis int i0, i1, i2, i3, i4, i5, i6, i7, i8, i9;
200e93f7393Sniklas #endif
201*b725ae77Skettenis {
202*b725ae77Skettenis return
203*b725ae77Skettenis (i0 == 0) && (i1 == 1) && (i2 == 2) && (i3 == 3) && (i4 == 4) &&
204*b725ae77Skettenis (i5 == 5) && (i6 == 6) && (i7 == 7) && (i8 == 8) && (i9 == 9);
205e93f7393Sniklas }
206e93f7393Sniklas
207e93f7393Sniklas /* Functions that expect specific values to be passed and return
208e93f7393Sniklas either 0 or 1, depending upon whether the values were
209e93f7393Sniklas passed incorrectly or correctly, respectively. */
210e93f7393Sniklas
211*b725ae77Skettenis #ifdef PROTOTYPES
t_char_values(char char_arg1,char char_arg2)212*b725ae77Skettenis int t_char_values (char char_arg1, char char_arg2)
213*b725ae77Skettenis #else
214e93f7393Sniklas int t_char_values (char_arg1, char_arg2)
215e93f7393Sniklas char char_arg1, char_arg2;
216*b725ae77Skettenis #endif
217e93f7393Sniklas {
218e93f7393Sniklas return ((char_arg1 == char_val1) && (char_arg2 == char_val2));
219e93f7393Sniklas }
220e93f7393Sniklas
221e93f7393Sniklas int
222*b725ae77Skettenis #ifdef PROTOTYPES
t_small_values(char arg1,short arg2,int arg3,char arg4,short arg5,char arg6,short arg7,int arg8,short arg9,short arg10)223*b725ae77Skettenis t_small_values (char arg1, short arg2, int arg3, char arg4, short arg5,
224*b725ae77Skettenis char arg6, short arg7, int arg8, short arg9, short arg10)
225*b725ae77Skettenis #else
226e93f7393Sniklas t_small_values (arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10)
227e93f7393Sniklas char arg1;
228e93f7393Sniklas short arg2;
229e93f7393Sniklas int arg3;
230e93f7393Sniklas char arg4;
231e93f7393Sniklas short arg5;
232e93f7393Sniklas char arg6;
233e93f7393Sniklas short arg7;
234e93f7393Sniklas int arg8;
235e93f7393Sniklas short arg9;
236e93f7393Sniklas short arg10;
237e93f7393Sniklas #endif
238e93f7393Sniklas {
239e93f7393Sniklas return arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10;
240e93f7393Sniklas }
241e93f7393Sniklas
242*b725ae77Skettenis #ifdef PROTOTYPES
t_short_values(short short_arg1,short short_arg2)243*b725ae77Skettenis int t_short_values (short short_arg1, short short_arg2)
244*b725ae77Skettenis #else
245e93f7393Sniklas int t_short_values (short_arg1, short_arg2)
246e93f7393Sniklas short short_arg1, short_arg2;
247*b725ae77Skettenis #endif
248e93f7393Sniklas {
249e93f7393Sniklas return ((short_arg1 == short_val1) && (short_arg2 == short_val2));
250e93f7393Sniklas }
251e93f7393Sniklas
252*b725ae77Skettenis #ifdef PROTOTYPES
t_int_values(int int_arg1,int int_arg2)253*b725ae77Skettenis int t_int_values (int int_arg1, int int_arg2)
254*b725ae77Skettenis #else
255e93f7393Sniklas int t_int_values (int_arg1, int_arg2)
256e93f7393Sniklas int int_arg1, int_arg2;
257*b725ae77Skettenis #endif
258e93f7393Sniklas {
259e93f7393Sniklas return ((int_arg1 == int_val1) && (int_arg2 == int_val2));
260e93f7393Sniklas }
261e93f7393Sniklas
262*b725ae77Skettenis #ifdef PROTOTYPES
t_long_values(long long_arg1,long long_arg2)263*b725ae77Skettenis int t_long_values (long long_arg1, long long_arg2)
264*b725ae77Skettenis #else
265e93f7393Sniklas int t_long_values (long_arg1, long_arg2)
266e93f7393Sniklas long long_arg1, long_arg2;
267*b725ae77Skettenis #endif
268e93f7393Sniklas {
269e93f7393Sniklas return ((long_arg1 == long_val1) && (long_arg2 == long_val2));
270e93f7393Sniklas }
271e93f7393Sniklas
272*b725ae77Skettenis /* NOTE: THIS FUNCTION MUST NOT BE PROTOTYPED!!!!!
273*b725ae77Skettenis There must be one version of "t_float_values" (this one)
274*b725ae77Skettenis that is not prototyped, and one (if supported) that is (following).
275*b725ae77Skettenis That way GDB can be tested against both cases. */
276*b725ae77Skettenis
t_float_values(float_arg1,float_arg2)277e93f7393Sniklas int t_float_values (float_arg1, float_arg2)
278e93f7393Sniklas float float_arg1, float_arg2;
279e93f7393Sniklas {
280e93f7393Sniklas return ((float_arg1 - float_val1) < DELTA
281e93f7393Sniklas && (float_arg1 - float_val1) > -DELTA
282e93f7393Sniklas && (float_arg2 - float_val2) < DELTA
283e93f7393Sniklas && (float_arg2 - float_val2) > -DELTA);
284e93f7393Sniklas }
285e93f7393Sniklas
286e93f7393Sniklas int
287e93f7393Sniklas #ifdef NO_PROTOTYPES
288e93f7393Sniklas /* In this case we are just duplicating t_float_values, but that is the
289e93f7393Sniklas easiest way to deal with either ANSI or non-ANSI. */
t_float_values2(float_arg1,float_arg2)290e93f7393Sniklas t_float_values2 (float_arg1, float_arg2)
291e93f7393Sniklas float float_arg1, float_arg2;
292e93f7393Sniklas #else
293e93f7393Sniklas t_float_values2 (float float_arg1, float float_arg2)
294e93f7393Sniklas #endif
295e93f7393Sniklas {
296e93f7393Sniklas return ((float_arg1 - float_val1) < DELTA
297e93f7393Sniklas && (float_arg1 - float_val1) > -DELTA
298e93f7393Sniklas && (float_arg2 - float_val2) < DELTA
299e93f7393Sniklas && (float_arg2 - float_val2) > -DELTA);
300e93f7393Sniklas }
301e93f7393Sniklas
302*b725ae77Skettenis #ifdef PROTOTYPES
t_double_values(double double_arg1,double double_arg2)303*b725ae77Skettenis int t_double_values (double double_arg1, double double_arg2)
304*b725ae77Skettenis #else
305e93f7393Sniklas int t_double_values (double_arg1, double_arg2)
306e93f7393Sniklas double double_arg1, double_arg2;
307*b725ae77Skettenis #endif
308e93f7393Sniklas {
309e93f7393Sniklas return ((double_arg1 - double_val1) < DELTA
310e93f7393Sniklas && (double_arg1 - double_val1) > -DELTA
311e93f7393Sniklas && (double_arg2 - double_val2) < DELTA
312e93f7393Sniklas && (double_arg2 - double_val2) > -DELTA);
313e93f7393Sniklas }
314e93f7393Sniklas
315*b725ae77Skettenis #ifdef PROTOTYPES
t_string_values(char * string_arg1,char * string_arg2)316*b725ae77Skettenis int t_string_values (char *string_arg1, char *string_arg2)
317*b725ae77Skettenis #else
318e93f7393Sniklas int t_string_values (string_arg1, string_arg2)
319e93f7393Sniklas char *string_arg1, *string_arg2;
320*b725ae77Skettenis #endif
321e93f7393Sniklas {
322e93f7393Sniklas return (!strcmp (string_arg1, string_val1) &&
323e93f7393Sniklas !strcmp (string_arg2, string_val2));
324e93f7393Sniklas }
325e93f7393Sniklas
326*b725ae77Skettenis #ifdef PROTOTYPES
t_char_array_values(char char_array_arg1[],char char_array_arg2[])327*b725ae77Skettenis int t_char_array_values (char char_array_arg1[], char char_array_arg2[])
328*b725ae77Skettenis #else
329e93f7393Sniklas int t_char_array_values (char_array_arg1, char_array_arg2)
330e93f7393Sniklas char char_array_arg1[], char_array_arg2[];
331*b725ae77Skettenis #endif
332e93f7393Sniklas {
333e93f7393Sniklas return (!strcmp (char_array_arg1, char_array_val1) &&
334e93f7393Sniklas !strcmp (char_array_arg2, char_array_val2));
335e93f7393Sniklas }
336e93f7393Sniklas
337e93f7393Sniklas
338e93f7393Sniklas /* This used to simply compare the function pointer arguments with
339e93f7393Sniklas known values for func_val1 and func_val2. Doing so is valid ANSI
340e93f7393Sniklas code, but on some machines (RS6000, HPPA, others?) it may fail when
341e93f7393Sniklas called directly by GDB.
342e93f7393Sniklas
343e93f7393Sniklas In a nutshell, it's not possible for GDB to determine when the address
344e93f7393Sniklas of a function or the address of the function's stub/trampoline should
345e93f7393Sniklas be passed.
346e93f7393Sniklas
347e93f7393Sniklas So, to avoid GDB lossage in the common case, we perform calls through the
348e93f7393Sniklas various function pointers and compare the return values. For the HPPA
349e93f7393Sniklas at least, this allows the common case to work.
350e93f7393Sniklas
351e93f7393Sniklas If one wants to try something more complicated, pass the address of
352e93f7393Sniklas a function accepting a "double" as one of its first 4 arguments. Call
353e93f7393Sniklas that function indirectly through the function pointer. This would fail
354e93f7393Sniklas on the HPPA. */
355e93f7393Sniklas
356*b725ae77Skettenis #ifdef PROTOTYPES
t_func_values(int (* func_arg1)(int,int),int (* func_arg2)(int))357*b725ae77Skettenis int t_func_values (int (*func_arg1)(int, int), int (*func_arg2)(int))
358*b725ae77Skettenis #else
359e93f7393Sniklas int t_func_values (func_arg1, func_arg2)
360e93f7393Sniklas int (*func_arg1) PARAMS ((int, int));
361e93f7393Sniklas int (*func_arg2) PARAMS ((int));
362*b725ae77Skettenis #endif
363e93f7393Sniklas {
364e93f7393Sniklas return ((*func_arg1) (5,5) == (*func_val1) (5,5)
365e93f7393Sniklas && (*func_arg2) (6) == (*func_val2) (6));
366e93f7393Sniklas }
367e93f7393Sniklas
368*b725ae77Skettenis #ifdef PROTOTYPES
t_call_add(int (* func_arg1)(int,int),int a,int b)369*b725ae77Skettenis int t_call_add (int (*func_arg1)(int, int), int a, int b)
370*b725ae77Skettenis #else
371e93f7393Sniklas int t_call_add (func_arg1, a, b)
372e93f7393Sniklas int (*func_arg1) PARAMS ((int, int));
373e93f7393Sniklas int a, b;
374*b725ae77Skettenis #endif
375e93f7393Sniklas {
376e93f7393Sniklas return ((*func_arg1)(a, b));
377e93f7393Sniklas }
378*b725ae77Skettenis
379*b725ae77Skettenis
380*b725ae77Skettenis /* Gotta have a main to be able to generate a linked, runnable
381*b725ae77Skettenis executable, and also provide a useful place to set a breakpoint. */
382*b725ae77Skettenis
main()383*b725ae77Skettenis int main ()
384*b725ae77Skettenis {
385*b725ae77Skettenis #ifdef usestubs
386*b725ae77Skettenis set_debug_traps();
387*b725ae77Skettenis breakpoint();
388*b725ae77Skettenis #endif
389*b725ae77Skettenis malloc(1);
390*b725ae77Skettenis t_double_values(double_val1, double_val2);
391*b725ae77Skettenis t_structs_c(struct_val1);
392*b725ae77Skettenis return 0 ;
393*b725ae77Skettenis }
394