Fix segfault with 'set print object on' + 'whatis <struct>' & co
[external/binutils.git] / gdb / testsuite / gdb.base / whatis.exp
1 # Copyright 1988-2018 Free Software Foundation, Inc.
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 # This file was written by Rob Savoye. (rob@cygnus.com)
17
18 #
19 # test running programs
20 #
21
22 if [target_info exists no_long_long] {
23     set exec_opts [list debug additional_flags=-DNO_LONG_LONG]
24 } else {
25     set exec_opts [list debug]
26 }
27
28 standard_testfile .c
29
30 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable $exec_opts] != "" } {
31      untested "failed to compile"
32      return -1
33 }
34
35 # Create and source the file that provides information about the compiler
36 # used to compile the test case.
37 if [get_compiler_info] {
38     return -1
39 }
40
41 # Start with a fresh gdb.
42
43 clean_restart $binfile
44
45 # Define a procedure to set up an xfail for all targets that put out a
46 # `long' type as an `int' type.
47 # Sun cc has this problem.
48 # It was said that COFF targets can not distinguish int from long either.
49
50 proc setup_xfail_on_long_vs_int {} {
51     global gcc_compiled
52
53     if {!$gcc_compiled} {
54         setup_xfail "*-sun-sunos4*" "i*86-sequent-bsd*"
55     }
56 }
57
58 #
59 # Test whatis command with basic C types
60 #
61 # The name printed now (as of 23 May 1993) is whatever name the compiler
62 # uses in the stabs.  So we need to deal with names both from gcc and
63 # native compilers.
64 #
65
66 gdb_test "whatis v_char" \
67     "type = (unsigned char|char)" \
68     "whatis char"
69
70 gdb_test "whatis v_signed_char" \
71     "type = (signed char|char)" \
72     "whatis signed char"
73
74 gdb_test "whatis v_unsigned_char" \
75     "type = unsigned char" \
76     "whatis unsigned char"
77
78 gdb_test "whatis v_short" \
79     "type = (short|short int)" \
80     "whatis short"
81
82 gdb_test "whatis v_signed_short" \
83     "type = (short|short int|signed short|signed short int)" \
84     "whatis signed short"
85
86 gdb_test "whatis v_unsigned_short" \
87     "type = (unsigned short|short unsigned int)" \
88     "whatis unsigned short" 
89
90 gdb_test "whatis v_int" \
91     "type = int" \
92     "whatis int"
93
94 gdb_test "whatis v_signed_int" \
95     "type = (signed |)int" \
96     "whatis signed int"
97
98 gdb_test "whatis v_unsigned_int" \
99     "type = unsigned int" \
100     "whatis unsigned int"
101
102 setup_xfail_on_long_vs_int
103 # AIX xlc gets this wrong and unsigned long right.  Go figure.
104 if {!$gcc_compiled} then {setup_xfail "rs6000-*-aix*"}
105 gdb_test "whatis v_long" \
106     "type = (long|long int)" \
107     "whatis long"
108
109 setup_xfail_on_long_vs_int
110 # AIX xlc gets this wrong and unsigned long right.  Go figure.
111 if {!$gcc_compiled} then {setup_xfail "rs6000-*-aix*"}
112 gdb_test "whatis v_signed_long" \
113     "type = (signed |)(long|long int)" \
114     "whatis signed long"
115
116 setup_xfail_on_long_vs_int
117 gdb_test "whatis v_unsigned_long" \
118     "type = (unsigned long|long unsigned int)" \
119     "whatis unsigned long"
120
121
122 if ![target_info exists no_long_long] {
123     gdb_test "whatis v_unsigned_long_long" \
124         "type = (unsigned long long|long long unsigned int)" \
125         "whatis unsigned long long"
126 }
127
128 gdb_test "whatis v_float" \
129     "type = float" \
130     "whatis float"
131
132 gdb_test "whatis v_double" \
133     "type = double" \
134     "whatis double"
135
136
137 # test whatis command with arrays
138 #
139 # We already tested whether char prints as "char", so here we accept
140 # "unsigned char", "signed char", and other perversions.  No need for more
141 # than one xfail for the same thing.
142 gdb_test "whatis v_char_array" \
143     "type = (signed |unsigned |)char \\\[2\\\]" \
144     "whatis char array"
145
146 gdb_test "whatis v_signed_char_array" \
147     "type = (signed |unsigned |)char \\\[2\\\]" \
148     "whatis signed char array"
149
150 gdb_test "whatis v_unsigned_char_array" \
151     "type = unsigned char \\\[2\\\]" \
152     "whatis unsigned char array"
153
154 gdb_test "whatis v_short_array" \
155     "type = (short|short int) \\\[2\\\]" \
156     "whatis short array"
157
158 gdb_test "whatis v_signed_short_array" \
159     "type = (signed |)(short|short int) \\\[2\\\]" \
160     "whatis signed short array"
161
162 gdb_test "whatis v_unsigned_short_array" \
163     "type = (unsigned short|short unsigned int) \\\[2\\\]" \
164     "whatis unsigned short array"
165
166 gdb_test "whatis v_int_array" \
167     "type = int \\\[2\\\]" \
168     "whatis int array"
169
170 gdb_test "whatis v_signed_int_array" \
171     "type = (signed |)int \\\[2\\\]" \
172     "whatis signed int array"
173
174 gdb_test "whatis v_unsigned_int_array" \
175     "type = unsigned int \\\[2\\\]" \
176     "whatis unsigned int array"
177
178 # We already tested whether long prints as long, so here we accept int
179 # No need for more than one xfail for the same thing.
180 gdb_test "whatis v_long_array" \
181     "type = (int|long|long int) \\\[2\\\]" \
182     "whatis long array"
183
184 gdb_test "whatis v_signed_long_array" \
185     "type = (signed |)(int|long|long int) \\\[2\\\]" \
186     "whatis signed long array"
187
188 gdb_test "whatis v_unsigned_long_array" \
189     "type = (unsigned (int|long|long int)|long unsigned int) \\\[2\\\]" \
190     "whatis unsigned long array"
191
192 if ![target_info exists no_long_long] {
193     gdb_test "whatis v_unsigned_long_long_array" \
194         "type = (unsigned long long|long long unsigned int) \\\[2\\\]" \
195         "whatis unsigned long array"
196 }
197
198 gdb_test "whatis v_float_array" \
199     "type = float \\\[2\\\]" \
200     "whatis float array"
201
202 gdb_test "whatis v_double_array" \
203     "type = double \\\[2\\\]" \
204     "whatis double array"
205
206
207 # test whatis command with pointers
208 #
209 # We already tested whether char prints as char, so accept various perversions
210 # here.  We especially want to make sure we test that it doesn't print as
211 # caddr_t.
212 gdb_test "whatis v_char_pointer" \
213     "type = (unsigned |signed |)char \\*" \
214     "whatis char pointer"
215
216 gdb_test "whatis v_signed_char_pointer" \
217     "type = (unsigned |signed |)char \\*" \
218     "whatis signed char pointer"
219
220 gdb_test "whatis v_unsigned_char_pointer" \
221     "type = unsigned char \\*" \
222     "whatis unsigned char pointer"
223
224 gdb_test "whatis v_short_pointer" \
225     "type = (short|short int) \\*" \
226     "whatis short pointer"
227
228 gdb_test "whatis v_signed_short_pointer" \
229     "type = (signed |)(short|short int) \\*" \
230     "whatis signed short pointer"
231
232 gdb_test "whatis v_unsigned_short_pointer" \
233     "type = (unsigned short|short unsigned int) \\*" \
234     "whatis unsigned short pointer"
235
236 gdb_test "whatis v_int_pointer" \
237     "type = int \\*" \
238     "whatis int pointer"
239
240 gdb_test "whatis v_signed_int_pointer" \
241     "type = (signed |)int \\*" \
242     "whatis signed int pointer"
243
244 gdb_test "whatis v_unsigned_int_pointer" \
245     "type = unsigned int \\*" \
246     "whatis unsigned int pointer"
247
248 # We already tested whether long prints as long, so here we accept int
249 gdb_test "whatis v_long_pointer" \
250     "type = (long|int|long int) \\*" \
251     "whatis long pointer"
252
253 gdb_test "whatis v_signed_long_pointer" \
254     "type = (signed |)(long|int|long int) \\*" \
255     "whatis signed long pointer"
256
257 gdb_test "whatis v_unsigned_long_pointer" \
258     "type = (unsigned (int|long|long int)|long unsigned int) \\*" \
259     "whatis unsigned long pointer"
260
261 if ![target_info exists no_long_long] {
262     gdb_test "whatis v_long_long_pointer" \
263         "type = long long(| int) \\*" \
264         "whatis long long pointer"
265
266     gdb_test "whatis v_signed_long_long_pointer" \
267         "type = (signed |)long long(| int) \\*" \
268         "whatis signed long long pointer"
269
270     gdb_test "whatis v_unsigned_long_long_pointer" \
271         "type = (unsigned long long|long long unsigned int) \\*" \
272         "whatis unsigned long long pointer"
273 }
274
275 gdb_test "whatis v_float_pointer" \
276     "type = float \\*" \
277     "whatis float pointer"
278
279 gdb_test "whatis v_double_pointer" \
280     "type = double \\*" \
281     "whatis double pointer"
282
283
284 # test whatis command with structure types
285
286 # First with a type argument, with both "set print object" set to "on"
287 # and "off", ending with "off" for the following tests.
288 foreach_with_prefix print_object {"on" "off"} {
289     gdb_test_no_output "set print object $print_object"
290
291     gdb_test "whatis struct t_struct" \
292         "type = struct t_struct" \
293         "whatis named structure using type name"
294
295     gdb_test "whatis struct t_struct *" \
296         "type = struct t_struct \\*" \
297         "whatis named structure using type name and pointer"
298
299     gdb_test "whatis struct t_struct &" \
300         "type = struct t_struct &" \
301         "whatis named structure using type name and reference"
302 }
303
304 # Now with an expression argument.
305
306 gdb_test "whatis v_struct1" \
307     "type = struct t_struct" \
308     "whatis named structure"
309
310 gdb_test "whatis v_struct2" \
311     "type = struct \{\.\.\.\}" \
312     "whatis unnamed structure"
313
314 gdb_test "whatis &v_struct1" \
315     "type = struct t_struct \\*"
316
317 gdb_test "whatis &v_struct2" \
318     "type = struct {\\.\\.\\.} \\*"
319
320 gdb_test "whatis v_struct_ptr1" \
321     "type = struct t_struct \\*"
322
323 gdb_test "whatis v_struct_ptr2" \
324     "type = struct {\\.\\.\\.} \\*"
325
326 gdb_test "whatis &v_struct_ptr1" \
327     "type = struct t_struct \\*\\*"
328
329 gdb_test "whatis &v_struct_ptr2" \
330     "type = struct {\\.\\.\\.} \\*\\*"
331
332 gdb_test "whatis v_struct1.v_char_member" \
333     "type = char"
334
335 gdb_test "whatis v_struct2.v_char_member" \
336     "type = char"
337
338 gdb_test "whatis v_struct_ptr1->v_char_member" \
339     "type = char"
340
341 gdb_test "whatis v_struct_ptr2->v_char_member" \
342     "type = char"
343
344 gdb_test "whatis &(v_struct1.v_char_member)" \
345     "type = char \\*"
346
347 gdb_test "whatis &(v_struct2.v_char_member)" \
348     "type = char \\*"
349
350 gdb_test "whatis &(v_struct_ptr1->v_char_member)" \
351     "type = char \\*"
352
353 gdb_test "whatis &(v_struct_ptr2->v_char_member)" \
354     "type = char \\*"
355
356 # test whatis command with union types
357 gdb_test "whatis v_union" \
358     "type = union t_union" \
359     "whatis named union"
360
361 gdb_test "whatis union t_union" \
362     "type = union t_union" \
363     "whatis named union using type name"
364
365 gdb_test "whatis v_union2" \
366     "type = union \{\.\.\.\}" \
367     "whatis unnamed union"
368
369 gdb_test "whatis &v_union" \
370     "type = union t_union \\*"
371
372 gdb_test "whatis &v_union2" \
373     "type = union {\\.\\.\\.} \\*"
374
375 gdb_test "whatis v_union_ptr" \
376     "type = union t_union \\*"
377
378 gdb_test "whatis v_union_ptr2" \
379     "type = union {\\.\\.\\.} \\*"
380
381 gdb_test "whatis &v_union_ptr" \
382     "type = union t_union \\*\\*"
383
384 gdb_test "whatis &v_union_ptr2" \
385     "type = union {\\.\\.\\.} \\*\\*"
386
387 gdb_test "whatis v_union.v_char_member" \
388     "type = char"
389
390 gdb_test "whatis v_union2.v_char_member" \
391     "type = char"
392
393 gdb_test "whatis v_union_ptr->v_char_member" \
394     "type = char"
395
396 gdb_test "whatis v_union_ptr2->v_char_member" \
397     "type = char"
398
399 gdb_test "whatis &(v_union.v_char_member)" \
400     "type = char \\*"
401
402 gdb_test "whatis &(v_union2.v_char_member)" \
403     "type = char \\*"
404
405 gdb_test "whatis &(v_union_ptr->v_char_member)" \
406     "type = char \\*"
407
408 gdb_test "whatis &(v_union_ptr2->v_char_member)" \
409     "type = char \\*"
410
411
412 # Using stabs we will mark these functions as prototyped.  This
413 # is harmless but causes an extra VOID to be printed.
414 set void "(void|)"
415
416 # test whatis command with functions return type
417 gdb_test "whatis v_char_func" \
418     "type = (signed |unsigned |)char \\($void\\)" \
419     "whatis char function"
420
421 gdb_test "whatis v_signed_char_func" \
422     "type = (signed |unsigned |)char \\($void\\)" \
423     "whatis signed char function"
424
425 gdb_test "whatis v_unsigned_char_func" \
426     "type = unsigned char \\($void\\)"  \
427     "whatis unsigned char function"
428
429 gdb_test "whatis v_short_func" \
430     "type = short (int |)\\($void\\)" \
431     "whatis short function"
432
433 gdb_test "whatis v_signed_short_func" \
434     "type = (signed |)short (int |)\\($void\\)" \
435     "whatis signed short function"
436
437 gdb_test "whatis v_unsigned_short_func" \
438     "type = (unsigned short|short unsigned int) \\($void\\)" \
439     "whatis unsigned short function"
440
441 gdb_test "whatis v_int_func" \
442     "type = int \\($void\\)" \
443     "whatis int function"
444
445 gdb_test "whatis v_signed_int_func" \
446     "type = (signed |)int \\($void\\)" \
447     "whatis signed int function"
448
449 gdb_test "whatis v_unsigned_int_func" \
450     "type = unsigned int \\($void\\)" \
451     "whatis unsigned int function"
452
453 gdb_test "whatis v_long_func" \
454     "type = (long|int|long int) \\($void\\)" \
455     "whatis long function"
456
457 gdb_test "whatis v_signed_long_func" \
458     "type = (signed |)(int|long|long int) \\($void\\)" \
459     "whatis signed long function"
460
461 gdb_test "whatis v_unsigned_long_func" \
462     "type = (unsigned (int|long|long int)|long unsigned int) \\($void\\)" \
463     "whatis unsigned long function"
464
465 if ![target_info exists no_long_long] {
466     gdb_test "whatis v_long_long_func" \
467         "type = long long(| int) \\($void\\)" \
468         "whatis long long function"
469
470     gdb_test "whatis v_signed_long_long_func" \
471         "type = (signed |)long long(| int) \\($void\\)" \
472         "whatis signed long long function"
473
474     gdb_test "whatis v_unsigned_long_long_func" \
475         "type = (unsigned long long(| int)|long long unsigned int) \\($void\\)" \
476         "whatis unsigned long long function"
477 }
478
479 # Sun /bin/cc calls this a function returning double.
480 if {!$gcc_compiled} then {setup_xfail "*-sun-sunos4*"}
481 gdb_test "whatis v_float_func" \
482     "type = float \\($void\\)" \
483     "whatis float function"
484
485 gdb_test "whatis v_double_func" \
486     "type = double \\($void\\)" \
487     "whatis double function" \
488
489
490 # test whatis command with some misc complicated types
491 gdb_test "whatis s_link" \
492     "type = struct link \\*" \
493     "whatis complicated structure"
494
495 gdb_test "whatis u_link" \
496     "type = union tu_link" \
497     "whatis complicated union"
498
499
500 # test whatis command with enumerations
501 gdb_test "whatis clunker" \
502     "type = enum cars" \
503     "whatis enumeration"
504
505 gdb_test "whatis enum cars" \
506     "type = enum cars" \
507     "whatis enumeration using type name"
508
509
510 # test whatis command with nested struct and union
511 gdb_test "whatis nested_su" \
512     "type = struct outer_struct" \
513     "whatis outer structure"
514
515 gdb_test "whatis nested_su.outer_int" \
516     "type = int" \
517     "whatis outer structure member"
518
519 gdb_test "whatis nested_su.inner_struct_instance" \
520     "type = struct inner_struct" \
521     "whatis inner structure"
522
523 gdb_test "whatis nested_su.inner_struct_instance.inner_int" \
524     "type = int" \
525     "whatis inner structure member"
526
527 gdb_test "whatis nested_su.inner_union_instance" \
528     "type = union inner_union" \
529     "whatis inner union"
530
531 gdb_test "whatis nested_su.inner_union_instance.inner_union_int" \
532     "type = int" \
533     "whatis inner union member"
534
535 # test whatis command with typedefs
536
537 gdb_test "whatis char_addr" \
538     "type = char \\*" \
539     "whatis using typedef type name"
540
541 gdb_test "whatis a_char_addr" \
542     "type = char_addr" \
543     "whatis applied to variable defined by typedef"
544
545 # Regression tests for PR 9514.
546
547 gdb_test "whatis void (**)()" \
548   "type = void \\(\\*\\*\\)\\(\\)" \
549   "whatis applied to pointer to pointer to function"
550
551 gdb_test "whatis void (** const)()" \
552   "type = void \\(\\*\\* const\\)\\(\\)" \
553   "whatis applied to const pointer to pointer to function"
554
555 gdb_test "whatis void (* const *)()" \
556   "type = void \\(\\* const \\*\\)\\(\\)" \
557   "whatis applied to pointer to const pointer to function"
558
559 gdb_test "whatis int *(*)()" \
560     "type = int \\*\\(\\*\\)\\(\\)" \
561     "whatis applied to pointer to function returning pointer to int"
562
563 gdb_test "whatis int *(**)()" \
564     "type = int \\*\\(\\*\\*\\)\\(\\)" \
565     "whatis applied to pointer to pointer to function returning pointer to int"
566
567 gdb_test "whatis char (*(*)())\[23\]" \
568     "type = char \\(\\*\\(\\*\\)\\(\\)\\)\\\[23\\\]" \
569     "whatis applied to pointer to function returning pointer to array"
570
571 gdb_test "whatis int (*)(int, int)" \
572     "type = int \\(\\*\\)\\(int, int\\)" \
573     "whatis applied to pointer to function taking int,int and returning int"
574
575 gdb_test "whatis int (*)(const int *, ...)" \
576     "type = int \\(\\*\\)\\(const int \\*, \\.\\.\\.\\)" \
577     "whatis applied to pointer to function taking const int ptr and varargs and returning int"
578
579 gdb_test "whatis int (*)(void, int, int)" \
580     "parameter types following 'void'" \
581     "whatis applied to function with types trailing 'void'"
582
583 gdb_test "whatis int (*)(int, void, int)" \
584     "'void' invalid as parameter type" \
585     "whatis applied to function with 'void' parameter type"