summaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.base/infcall-nested-structs.c
blob: f2a42945cee6ce3b389391bd5e55ecde3665c7df (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/* This testcase is part of GDB, the GNU debugger.

   Copyright 2018-2019 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/>.  */

/* This file is used for testing GDBs ability to pass structures to, and
   return structures from, functions.  All of the structures in this test
   are special in that they are small structures containing from 1 up to 5
   scalar fields, the fields can be inside nested structures, and there can
   be empty structures around too.

   When compiled for C++ this file also tests structures containing static
   members (which live in global memory).  In addition, empty structures in C++
   have a size of 1 (compared to 0 in GNU C), which can effect structure
   padding.

   This test is specifically written for RiscV and Aarch64, which both have
   special ABI rules for structures like these, however, there should be no harm
   in running these tests on other targets, though in many cases the
   structures will treated no differently to the structures already covered
   in the structs.exp test script.  */

#include <string.h>

/* Useful abreviations.  */
typedef char tc;
typedef short ts;
typedef int ti;
typedef long tl;
typedef long long tll;
typedef float tf;
typedef double td;
typedef long double tld;

#ifdef TEST_COMPLEX
typedef float _Complex tfc;
typedef double _Complex tdc;
typedef long double _Complex tldc;
#endif /* TEST_COMPLEX */

#define MAKE_CHECK_FUNCS(TYPE)					\
  int __attribute__((noinline,noclone))				\
  check_arg_ ## TYPE (struct TYPE arg)				\
  {								\
    return cmp_ ## TYPE (arg, ref_val_ ## TYPE);		\
  }								\
								\
  struct TYPE __attribute__((noinline,noclone))			\
  rtn_str_ ## TYPE (void)					\
  {								\
    return (ref_val_ ## TYPE);					\
  }

#define REF_VAL(NAME) struct NAME ref_val_ ## NAME
#define ES(NAME) struct { } NAME

/* Test is either for a single type or two differing types.  */
#if defined tA && ! defined tB
#define tB tA
#endif
#if ! defined tB
#error "Incorrect configuration of tA and tB defines"
#endif

/* Structures with a single field nested to various depths, along with
   some empty structures.  */
struct struct_01_01 { ES(es1); struct { struct { tA a; } s1; } s2; };
struct struct_01_02 { tA a; struct { struct { ES(es1); } s1; } s2; };
struct struct_01_03 { struct { struct { ES(es1); } s1; } s2; ES(es1); struct { struct { tA a; } s3; } s4;};
struct struct_01_04 { ES(es1); ES(es2); tA a; ES(es3); };

/* Structures with two fields nested to various depths, along with
   some empty structures.  */
struct struct_02_01 { ES(es1); struct { struct { tA a; tB b; } s1; } s2; };
struct struct_02_02 { tA a; struct { struct { ES(es1); } s1; } s2; tB b; };
struct struct_02_03 { struct { struct { ES(es1); } s1; } s2; ES(es1); struct { struct { tA a; } s3; } s4; struct { struct { tB b; } s5; } s6;};
struct struct_02_04 { ES(es1); ES(es2); tA a; ES(es3); tB b; };

/* Structures with four fields nested to various depths, along with
   some empty structures.  */
struct struct_04_01 { ES(es1); struct { struct { tA a; tB b; tA c; tB d; } s1; } s2; };
struct struct_04_02 { tA a; struct { struct { ES(es1); } s1; } s2; tB b; struct { struct { ES(es1); } s2; } s3; tA c; struct { struct { ES(es2); } s4; } s5; tB d;};
struct struct_04_03 { struct { struct { ES(es1); } s1; } s2; ES(es1); struct { struct { tA a; } s3; } s4; struct { struct { tB b; } s5; } s6; struct { struct { tA c; } s7; } s8; struct { struct { tB d; } s9; } s10;};
struct struct_04_04 { ES(es1); ES(es2); tA a; ES(es3); tB b; ES(es4); tA c; ES(es5); tB d; };

/* Structures with five fields nested to various depths, along with
   some empty structures.  */
struct struct_05_01 { ES(es1); struct { struct { tA a; tB b; tA c; tB d; tA e; } s1; } s2; };
struct struct_05_02 { tA a; struct { struct { ES(es1); } s1; } s2; tB b; struct { struct { ES(es1); } s2; } s3; tA c; struct { struct { ES(es2); } s4; } s5; tB d; struct { struct { ES(es2); } s6; } s7; tB e;};
struct struct_05_03 { struct { struct { ES(es1); } s1; } s2; ES(es1); struct { struct { tA a; } s3; } s4; struct { struct { tB b; } s5; } s6; struct { struct { tA c; } s7; } s8; struct { struct { tB d; } s9; } s10; struct { struct { tA e; } s11; } s12;};
struct struct_05_04 { ES(es1); ES(es2); tA a; ES(es3); tB b; ES(es4); tA c; ES(es5); tB d; ES(es6); tA e; };

/* Only C++ allows structures to have static members.  */
#ifdef __cplusplus

/* Structures with two fields nested to various depths, one of which is static.
   Some include empty structures.  */
struct struct_static_02_01 { struct sa { struct sb { tA a; static tB b; } s1; } s2; };
struct struct_static_02_02 { static tA a; struct { struct { ES(es1); } s1; } s2; tB b; };
struct struct_static_02_03 { struct { struct { ES(es1); } s1; } s2; ES(es1); struct { struct { tA a; } s3; } s4; struct sa { struct sb { static tB b; } s5; } s6;};
struct struct_static_02_04 { static tA a; tB b; };

/* Structures with four fields nested to various depths, some of which are
   static.  Some include empty structures.  */
struct struct_static_04_01 { struct sa { struct sb { static tA a; tB b; tA c; tB d; } s1; } s2; };
struct struct_static_04_02 { tA a; struct { struct { ES(es1); } s1; } s2; tB b; struct { struct { ES(es1); } s2; } s3; static tA c; struct { struct { ES(es2); } s4; } s5; static tB d;};
struct struct_static_04_03 { struct sa { struct sb { static tA a; } s3; } s4; struct sc { struct sd { static tB b; } s5; } s6; struct se { struct sf { static tA c; } s7; } s8; struct sg { struct sh { static tB d; } s9; } s10;};
struct struct_static_04_04 { ES(es1); ES(es2); tA a; ES(es3); tB b; ES(es4); tA c; ES(es5); static tB d; };

/* Structures with six fields nested to various depths, some of which are
   static.  Some include empty structures.  */
struct struct_static_06_01 { struct sa { struct sb { tA a; static tB b; tA c; tB d; tA e; } s1; } s2; tB f; };
struct struct_static_06_02 { tA a; static tB b; static tA c; tB d; tB e; tA f;};
struct struct_static_06_03 { struct { struct { ES(es1); } s1; } s2; ES(es1); struct sa { struct sb { static tA a; } s3; } s4; struct sc { struct sd { tB b; } s5; } s6; struct se { struct sf { static tA c; } s7; } s8; struct sg { struct sh { static tB d; } s9; } s10; struct { struct { tA e; tB f; } s11; } s12;};
struct struct_static_06_04 { ES(es1); ES(es2); static tA a; ES(es3); static tB b; ES(es4); static tA c; ES(es5); static tB d; ES(es6); static tA e; ES(es7); tB f; };

#endif

int __attribute__((noinline,noclone))
cmp_struct_01_01 (struct struct_01_01 a, struct struct_01_01 b)
{ return a.s2.s1.a == b.s2.s1.a; }

int __attribute__((noinline,noclone))
cmp_struct_01_02 (struct struct_01_02 a, struct struct_01_02 b)
{ return a.a == b.a; }

int __attribute__((noinline,noclone))
cmp_struct_01_03 (struct struct_01_03 a, struct struct_01_03 b)
{ return a.s4.s3.a == b.s4.s3.a; }

int __attribute__((noinline,noclone))
cmp_struct_01_04 (struct struct_01_04 a, struct struct_01_04 b)
{ return a.a == b.a; }

int __attribute__((noinline,noclone))
cmp_struct_02_01 (struct struct_02_01 a, struct struct_02_01 b)
{ return a.s2.s1.a == b.s2.s1.a && a.s2.s1.b == b.s2.s1.b; }

int __attribute__((noinline,noclone))
cmp_struct_02_02 (struct struct_02_02 a, struct struct_02_02 b)
{ return a.a == b.a && a.b == b.b; }

int __attribute__((noinline,noclone))
cmp_struct_02_03 (struct struct_02_03 a, struct struct_02_03 b)
{ return a.s4.s3.a == b.s4.s3.a && a.s6.s5.b == b.s6.s5.b; }

int __attribute__((noinline,noclone))
cmp_struct_02_04 (struct struct_02_04 a, struct struct_02_04 b)
{ return a.a == b.a && a.b == b.b; }

int __attribute__((noinline,noclone))
cmp_struct_04_01 (struct struct_04_01 a, struct struct_04_01 b)
{ return a.s2.s1.a == b.s2.s1.a && a.s2.s1.b == b.s2.s1.b
	 && a.s2.s1.c == b.s2.s1.c && a.s2.s1.d == b.s2.s1.d; }

int __attribute__((noinline,noclone))
cmp_struct_04_02 (struct struct_04_02 a, struct struct_04_02 b)
{ return a.a == b.a && a.b == b.b && a.c == b.c && a.d == b.d; }

int __attribute__((noinline,noclone))
cmp_struct_04_03 (struct struct_04_03 a, struct struct_04_03 b)
{ return a.s4.s3.a == b.s4.s3.a && a.s6.s5.b == b.s6.s5.b
	 && a.s8.s7.c == b.s8.s7.c && a.s10.s9.d == b.s10.s9.d; }

int __attribute__((noinline,noclone))
cmp_struct_04_04 (struct struct_04_04 a, struct struct_04_04 b)
{ return a.a == b.a && a.b == b.b && a.c == b.c && a.d == b.d; }

int __attribute__((noinline,noclone))
cmp_struct_05_01 (struct struct_05_01 a, struct struct_05_01 b)
{ return a.s2.s1.a == b.s2.s1.a && a.s2.s1.b == b.s2.s1.b
	 && a.s2.s1.c == b.s2.s1.c && a.s2.s1.d == b.s2.s1.d
	 && a.s2.s1.e == b.s2.s1.e; }

int __attribute__((noinline,noclone))
cmp_struct_05_02 (struct struct_05_02 a, struct struct_05_02 b)
{ return a.a == b.a && a.b == b.b && a.c == b.c && a.d == b.d && a.e == b.e; }

int __attribute__((noinline,noclone))
cmp_struct_05_03 (struct struct_05_03 a, struct struct_05_03 b)
{ return a.s4.s3.a == b.s4.s3.a && a.s6.s5.b == b.s6.s5.b
	 && a.s8.s7.c == b.s8.s7.c && a.s10.s9.d == b.s10.s9.d
	 && a.s12.s11.e == b.s12.s11.e; }

int __attribute__((noinline,noclone))
cmp_struct_05_04 (struct struct_05_04 a, struct struct_05_04 b)
{ return a.a == b.a && a.b == b.b && a.c == b.c && a.d == b.d && a.e == b.e; }

#ifdef __cplusplus

int __attribute__((noinline,noclone))
cmp_struct_static_02_01 (struct struct_static_02_01 a,
			 struct struct_static_02_01 b)
{ return a.s2.s1.a == b.s2.s1.a && a.s2.s1.b == b.s2.s1.b; }

int __attribute__((noinline,noclone))
cmp_struct_static_02_02 (struct struct_static_02_02 a,
			 struct struct_static_02_02 b)
{ return a.a == b.a && a.b == b.b; }

int __attribute__((noinline,noclone))
cmp_struct_static_02_03 (struct struct_static_02_03 a,
			 struct struct_static_02_03 b)
{ return a.s4.s3.a == b.s4.s3.a && a.s6.s5.b == b.s6.s5.b; }

int __attribute__((noinline,noclone))
cmp_struct_static_02_04 (struct struct_static_02_04 a,
			     struct struct_static_02_04 b)
{ return a.a == b.a && a.b == b.b; }

int __attribute__((noinline,noclone))
cmp_struct_static_04_01 (struct struct_static_04_01 a,
			 struct struct_static_04_01 b)
{ return a.s2.s1.a == b.s2.s1.a && a.s2.s1.b == b.s2.s1.b
	 && a.s2.s1.c == b.s2.s1.c && a.s2.s1.d == b.s2.s1.d; }

int __attribute__((noinline,noclone))
cmp_struct_static_04_02 (struct struct_static_04_02 a,
			 struct struct_static_04_02 b)
{ return a.a == b.a && a.b == b.b && a.c == b.c && a.d == b.d; }

int __attribute__((noinline,noclone))
cmp_struct_static_04_03 (struct struct_static_04_03 a,
			 struct struct_static_04_03 b)
{ return a.s4.s3.a == b.s4.s3.a && a.s6.s5.b == b.s6.s5.b
	 && a.s8.s7.c == b.s8.s7.c && a.s10.s9.d == b.s10.s9.d; }

int __attribute__((noinline,noclone))
cmp_struct_static_04_04 (struct struct_static_04_04 a,
			 struct struct_static_04_04 b)
{ return a.a == b.a && a.b == b.b && a.c == b.c && a.d == b.d; }

int __attribute__((noinline,noclone))
cmp_struct_static_06_01 (struct struct_static_06_01 a,
			 struct struct_static_06_01 b)
{ return a.s2.s1.a == b.s2.s1.a && a.s2.s1.b == b.s2.s1.b
	 && a.s2.s1.c == b.s2.s1.c && a.s2.s1.d == b.s2.s1.d
	 && a.s2.s1.e == b.s2.s1.e && a.f == b.f; }

int __attribute__((noinline,noclone))
cmp_struct_static_06_02 (struct struct_static_06_02 a,
			 struct struct_static_06_02 b)
{ return a.a == b.a && a.b == b.b && a.c == b.c && a.d == b.d && a.e == b.e
	 && a.f == b.f; }

int __attribute__((noinline,noclone))
cmp_struct_static_06_03 (struct struct_static_06_03 a,
			 struct struct_static_06_03 b)
{ return a.s4.s3.a == b.s4.s3.a && a.s6.s5.b == b.s6.s5.b
	 && a.s8.s7.c == b.s8.s7.c && a.s10.s9.d == b.s10.s9.d
	 && a.s12.s11.e == b.s12.s11.e && a.s12.s11.f == b.s12.s11.f; }

int __attribute__((noinline,noclone))
cmp_struct_static_06_04 (struct struct_static_06_04 a,
			 struct struct_static_06_04 b)
{ return a.a == b.a && a.b == b.b && a.c == b.c && a.d == b.d && a.e == b.e
	 && a.f == b.f; }

#endif

REF_VAL(struct_01_01) = { {}, { { 'a' } } };
REF_VAL(struct_01_02) = { 'a', { { {} } } };
REF_VAL(struct_01_03) = { { { {} } }, {}, { { 'a' } } };
REF_VAL(struct_01_04) = { {}, {}, 'a', {} };

REF_VAL(struct_02_01) = { {}, { { 'a', 'b' } } };
REF_VAL(struct_02_02) = { 'a', { { {} } }, 'b' };
REF_VAL(struct_02_03) = { { { {} } }, {}, { { 'a' } }, { { 'b' } } };
REF_VAL(struct_02_04) = { {}, {}, 'a', {}, 'b' };

REF_VAL(struct_04_01) = { {}, { { 'a', 'b', 'c', 'd' } } };
REF_VAL(struct_04_02) = { 'a', { { {} } }, 'b', { { {} } }, 'c', { { {} } }, 'd' };
REF_VAL(struct_04_03) = { { { {} } }, {}, { { 'a' } }, { { 'b' } }, { { 'c' } }, { { 'd' } } };
REF_VAL(struct_04_04) = { {}, {}, 'a', {}, 'b', {}, 'c', {}, 'd' };

REF_VAL(struct_05_01) = { {}, { { 'a', 'b', 'c', 'd', 'e' } } };
REF_VAL(struct_05_02) = { 'a', { { {} } }, 'b', { { {} } }, 'c', { { {} } }, 'd', { { {} } }, 'e' };
REF_VAL(struct_05_03) = { { { {} } }, {}, { { 'a' } }, { { 'b' } }, { { 'c' } }, { { 'd' } }, { { 'e' } } };
REF_VAL(struct_05_04) = { {}, {}, 'a', {}, 'b', {}, 'c', {}, 'd', {}, 'e' };

#ifdef __cplusplus

/* Initialise static members.  */
tB struct_static_02_01::sa::sb::b = '1';
tA struct_static_02_02::a = '2';
tB struct_static_02_03::sa::sb::b = '3';
tA struct_static_02_04::a = '4';
tA struct_static_04_01::sa::sb::a = '5';
tA struct_static_04_02::c = '6';
tB struct_static_04_02::d = '7';
tA struct_static_04_03::sa::sb::a = '8';
tB struct_static_04_03::sc::sd::b = '9';
tA struct_static_04_03::se::sf::c = '0';
tB struct_static_04_03::sg::sh::d = 'A';
tB struct_static_04_04::d = 'B';
tB struct_static_06_01::sa::sb::b = 'C';
tB struct_static_06_02::b = 'D';
tA struct_static_06_02::c = 'E';
tA struct_static_06_03::sa::sb::a = 'F';
tA struct_static_06_03::se::sf::c = 'G';
tB struct_static_06_03::sg::sh::d = 'H';
tA struct_static_06_04::a = 'I';
tB struct_static_06_04::b = 'J';
tA struct_static_06_04::c = 'K';
tB struct_static_06_04::d = 'L';
tA struct_static_06_04::e = 'M';

REF_VAL(struct_static_02_01) = { { { 'a' } } };
REF_VAL(struct_static_02_02) = { { { {} } }, 'b' };
REF_VAL(struct_static_02_03) = { { { {} } }, {}, { { 'a' } }, { { } } };
REF_VAL(struct_static_02_04) = { 'b' };
REF_VAL(struct_static_04_01) = { { { 'b', 'c', 'd' } } };
REF_VAL(struct_static_04_02) = { 'a', { { {} } }, 'b', { { {} } }, { { {} } } };
REF_VAL(struct_static_04_03) = {};
REF_VAL(struct_static_04_04) = { {}, {}, 'a', {}, 'b', {}, 'c', {} };
REF_VAL(struct_static_06_01) = { { { 'a', 'c', 'd', 'e' } }, 'f' };
REF_VAL(struct_static_06_02) = { 'a', 'd', 'e', 'f' };
REF_VAL(struct_static_06_03) = { { { {} } }, {}, {}, { { 'b' } }, {}, /*{ { 'e', 'f' } }*/ };
REF_VAL(struct_static_06_04) = { {}, {}, {}, {}, {}, {}, {}, 'f' };

#endif

/* Create all of the functions GDB will call to check functionality.  */
MAKE_CHECK_FUNCS(struct_01_01)
MAKE_CHECK_FUNCS(struct_01_02)
MAKE_CHECK_FUNCS(struct_01_03)
MAKE_CHECK_FUNCS(struct_01_04)
MAKE_CHECK_FUNCS(struct_02_01)
MAKE_CHECK_FUNCS(struct_02_02)
MAKE_CHECK_FUNCS(struct_02_03)
MAKE_CHECK_FUNCS(struct_02_04)
MAKE_CHECK_FUNCS(struct_04_01)
MAKE_CHECK_FUNCS(struct_04_02)
MAKE_CHECK_FUNCS(struct_04_03)
MAKE_CHECK_FUNCS(struct_04_04)
MAKE_CHECK_FUNCS(struct_05_01)
MAKE_CHECK_FUNCS(struct_05_02)
MAKE_CHECK_FUNCS(struct_05_03)
MAKE_CHECK_FUNCS(struct_05_04)
#ifdef __cplusplus
MAKE_CHECK_FUNCS(struct_static_02_01)
MAKE_CHECK_FUNCS(struct_static_02_02)
MAKE_CHECK_FUNCS(struct_static_02_03)
MAKE_CHECK_FUNCS(struct_static_02_04)
MAKE_CHECK_FUNCS(struct_static_04_01)
MAKE_CHECK_FUNCS(struct_static_04_02)
MAKE_CHECK_FUNCS(struct_static_04_03)
MAKE_CHECK_FUNCS(struct_static_04_04)
MAKE_CHECK_FUNCS(struct_static_06_01)
MAKE_CHECK_FUNCS(struct_static_06_02)
MAKE_CHECK_FUNCS(struct_static_06_03)
MAKE_CHECK_FUNCS(struct_static_06_04)
#endif

#define CALL_LINE(NAME) val += check_arg_ ## NAME (rtn_str_ ## NAME ())

int __attribute__((noinline,noclone))
call_all ()
{
  int val = 0;

  CALL_LINE(struct_01_01);
  CALL_LINE(struct_01_02);
  CALL_LINE(struct_01_03);
  CALL_LINE(struct_01_04);
  CALL_LINE(struct_02_01);
  CALL_LINE(struct_02_02);
  CALL_LINE(struct_02_03);
  CALL_LINE(struct_02_04);
  CALL_LINE(struct_04_01);
  CALL_LINE(struct_04_02);
  CALL_LINE(struct_04_03);
  CALL_LINE(struct_04_04);
  CALL_LINE(struct_05_01);
  CALL_LINE(struct_05_02);
  CALL_LINE(struct_05_03);
  CALL_LINE(struct_05_04);
#ifdef __cplusplus
  CALL_LINE(struct_static_02_01);
  CALL_LINE(struct_static_02_02);
  CALL_LINE(struct_static_02_03);
  CALL_LINE(struct_static_02_04);
  CALL_LINE(struct_static_04_01);
  CALL_LINE(struct_static_04_02);
  CALL_LINE(struct_static_04_03);
  CALL_LINE(struct_static_04_04);
  CALL_LINE(struct_static_06_01);
  CALL_LINE(struct_static_06_02);
  CALL_LINE(struct_static_06_03);
  CALL_LINE(struct_static_06_04);
#endif

  return val;
}

int volatile v = 1;

void __attribute__((noinline, noclone))
breakpt (void)
{
  v++;
}

int
main ()
{
  int res;

  res = call_all ();
  breakpt (); /* Break Here.  */
  return res;
}