1 # Copyright 1992-2024 Free Software Foundation, Inc.
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.
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.
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/>.
16 # This file was written by Fred Fish. (fnf@cygnus.com)
17 # Adapted for g++ 3.0 ABI by Michael Chastain. (chastain@redhat.com)
19 require allow_cplus_tests
23 if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug c++}]} {
28 # g++ changed its ABI between 2.95 and 3.0. gdb has two demanglers
29 # for the two different styles. The two demanglers have some subtle
30 # discrepancies in their output.
32 # old demangler new demangler
33 # --- --------- --- ---------
34 # "operator, " "operator,"
40 # "unsigned int" "unsigned"
43 # I probe for the forms in use.
44 # The defaults are for the v3 demangler (as of 2001-02-13).
47 set dm_operator_comma ","
48 set dm_type_char_star "char*"
49 set dm_type_char_star_quoted "char\\*"
50 set dm_type_foo_ref "foo&"
51 set dm_type_int_star "int*"
52 set dm_type_long_star "long*"
53 set dm_type_unsigned_int "unsigned"
54 set dm_type_void "void"
55 set dm_type_void_star "void*"
57 # Some other vagaries of GDB's type printing machinery. The integer types
58 # may have unsigned before or after their length, and may have "int"
59 # appended. The char* conversion operator may have name "char*" even if
60 # the type is "char *", because the name comes from the debug information
61 # and the type from GDB. Function types may not see through typedefs.
63 set dm_type_short "short"
64 set dm_type_long "long"
65 set dm_type_unsigned_short "unsigned short"
66 set dm_type_unsigned_long "unsigned long"
67 set dm_operator_char_star "char*"
68 set dm_operator_char_star_quoted "char\\*"
71 proc probe_demangler { } {
73 global dm_operator_comma
74 global dm_operator_char_star
75 global dm_operator_char_star_quoted
76 global dm_type_char_star
77 global dm_type_char_star_quoted
78 global dm_type_foo_ref
79 global dm_type_int_star
80 global dm_type_long_star
81 global dm_type_unsigned_int
83 global dm_type_void_star
85 global dm_type_unsigned_short
87 global dm_type_unsigned_long
88 global dm_type_typedef
90 gdb_test_multiple "print &foo::operator,(foo&)" \
91 "detect dm_operator_comma" {
92 -re ".*foo::operator, \\(.*foo.*&.*\\).*\r\n$gdb_prompt $" {
94 set dm_operator_comma ", "
95 pass "detect dm_operator_comma"
97 -re ".*foo::operator,\\(.*foo.*&.*\\).*\r\n$gdb_prompt $" {
99 pass "detect dm_operator_comma"
103 gdb_test_multiple "print &foo::operator char*($dm_type_void)" \
104 "detect dm_operator_char_star" {
105 -re ".*foo::operator char \\*\\(void\\).*\r\n$gdb_prompt $" {
106 # v2 demangler or GDB type printer
107 set dm_operator_char_star "char *"
108 set dm_operator_char_star_quoted "char \\*"
109 pass "detect dm_operator_char_star"
111 -re ".*foo::operator char\\*\\(\\).*\r\n$gdb_prompt $" {
113 pass "detect dm_operator_char_star"
117 gdb_test_multiple "print &dm_type_char_star" \
118 "detect dm_type_char_star" {
119 -re ".*dm_type_char_star\\(char \\*\\).*\r\n$gdb_prompt $" {
121 set dm_type_char_star "char *"
122 set dm_type_char_star_quoted "char \\*"
123 pass "detect dm_type_char_star"
125 -re ".*dm_type_char_star\\(char\\*\\).*\r\n$gdb_prompt $" {
127 pass "detect dm_type_char_star"
131 gdb_test_multiple "print &dm_type_foo_ref" \
132 "detect dm_type_foo_ref" {
133 -re ".*dm_type_foo_ref\\(foo &\\).*\r\n$gdb_prompt $" {
135 set dm_type_foo_ref "foo &"
136 pass "detect dm_type_foo_ref"
138 -re ".*dm_type_foo_ref\\(foo&\\).*\r\n$gdb_prompt $" {
140 pass "detect dm_type_foo_ref"
144 gdb_test_multiple "print &dm_type_int_star" \
145 "detect dm_type_int_star" {
146 -re ".*dm_type_int_star\\(int \\*\\).*\r\n$gdb_prompt $" {
148 set dm_type_int_star "int *"
149 pass "detect dm_type_int_star"
151 -re ".*dm_type_int_star\\(int\\*\\).*\r\n$gdb_prompt $" {
153 pass "detect dm_type_int_star"
157 gdb_test_multiple "print &dm_type_long_star" \
158 "detect dm_type_long_star" {
159 -re ".*dm_type_long_star\\(long \\*\\).*\r\n$gdb_prompt $" {
161 set dm_type_long_star "long *"
162 pass "detect dm_type_long_star"
164 -re ".*dm_type_long_star\\(long\\*\\).*\r\n$gdb_prompt $" {
166 pass "detect dm_type_long_star"
168 -re ".*dm_type_long_star\\(long int \\*\\).*\r\n$gdb_prompt $" {
169 # GCC v3 and GDB's type printer
170 set dm_type_long_star "long int *"
171 pass "detect dm_type_long_star"
175 gdb_test_multiple "print &dm_type_unsigned_int" \
176 "detect dm_type_unsigned_int" {
177 -re ".*dm_type_unsigned_int\\(unsigned int\\).*\r\n$gdb_prompt $" {
179 set dm_type_unsigned_int "unsigned int"
180 pass "detect dm_type_unsigned_int"
182 -re ".*dm_type_unsigned_int\\(unsigned\\).*\r\n$gdb_prompt $" {
184 pass "detect dm_type_unsigned_int"
188 gdb_test_multiple "print &dm_type_void" \
189 "detect dm_type_void" {
190 -re ".*dm_type_void\\(void\\).*\r\n$gdb_prompt $" {
192 set dm_type_void "void"
193 pass "detect dm_type_void"
195 -re ".*dm_type_void\\(\\).*\r\n$gdb_prompt $" {
197 pass "detect dm_type_void"
201 gdb_test_multiple "print &dm_type_void_star" \
202 "detect dm_type_void_star" {
203 -re ".*dm_type_void_star\\(void \\*\\).*\r\n$gdb_prompt $" {
205 set dm_type_void_star "void *"
206 pass "detect dm_type_void_star"
208 -re ".*dm_type_void_star\\(void\\*\\).*\r\n$gdb_prompt $" {
210 pass "detect dm_type_void_star"
214 gdb_test_multiple "print &dm_type_short" \
215 "detect dm_type_short" {
216 -re ".*dm_type_short\\(short\\).*\r\n$gdb_prompt $" {
217 # v2 and v3 demanglers
218 pass "detect dm_type_short"
220 -re ".*dm_type_short\\(short int\\).*\r\n$gdb_prompt $" {
222 set dm_type_short "short int"
223 pass "detect dm_type_short"
227 gdb_test_multiple "print &dm_type_unsigned_short" \
228 "detect dm_type_unsigned_short" {
229 -re ".*dm_type_unsigned_short\\(unsigned short\\).*\r\n$gdb_prompt $" {
230 # v2 and v3 demanglers
231 pass "detect dm_type_unsigned_short"
233 -re ".*dm_type_unsigned_short\\(short unsigned int\\).*\r\n$gdb_prompt $" {
235 set dm_type_unsigned_short "short unsigned int"
236 pass "detect dm_type_unsigned_short"
240 gdb_test_multiple "print &dm_type_long" \
241 "detect dm_type_long" {
242 -re ".*dm_type_long\\(long\\).*\r\n$gdb_prompt $" {
243 # v2 and v3 demanglers
244 pass "detect dm_type_long"
246 -re ".*dm_type_long\\(long int\\).*\r\n$gdb_prompt $" {
248 set dm_type_long "long int"
249 pass "detect dm_type_long"
253 gdb_test_multiple "print &dm_type_unsigned_long" \
254 "detect dm_type_unsigned_long" {
255 -re ".*dm_type_unsigned_long\\(unsigned long\\).*\r\n$gdb_prompt $" {
256 # v2 and v3 demanglers
257 pass "detect dm_type_unsigned_long"
259 -re ".*dm_type_unsigned_long\\(long unsigned int\\).*\r\n$gdb_prompt $" {
261 set dm_type_unsigned_long "long unsigned int"
262 pass "detect dm_type_unsigned_long"
266 gdb_test_multiple "print &dm_type_typedef" \
267 "detect dm_type_typedef" {
268 -re ".*dm_type_typedef\\(int\\).*\r\n$gdb_prompt $" {
269 # v2 and v3 demanglers
270 pass "detect dm_type_typedef"
272 -re ".*dm_type_typedef\\(myint\\).*\r\n$gdb_prompt $" {
274 set dm_type_typedef 1
275 pass "detect dm_type_typedef"
281 # Lookup a specific C++ function and print the demangled type.
282 # This form accepts the demangled type as a regexp.
285 proc info_func_regexp { name demangled } {
286 global srcfile decimal
288 regsub {\\\(void\\\)} $demangled {\(\)} demangled
290 set file_re "File .*[string_to_regexp $srcfile]:"
292 gdb_test_lines "info function $name" "info function for \"$name\"" \
295 "$decimal:\t(class|)${demangled}.*"]
299 # Lookup a specific C++ function and print the demangled type.
300 # This form accepts the demangled type as a literal string.
303 proc info_func { name demangled } {
304 info_func_regexp "$name" [string_to_regexp "$demangled"]
308 # Print the address of a function.
309 # This checks that I can lookup a fully qualified C++ function.
310 # This also checks the argument types on the return string.
312 # Note: carlton/2003-01-16: If you modify this, make a corresponding
313 # modification to print_addr_2_kfail.
315 proc print_addr_2 { name good } {
319 set good_pattern [string_to_regexp $good]
321 gdb_test "print &$name" \
322 ".* = .* $hex <$good_pattern>"
325 # NOTE: carlton/2003-01-16: hairyfunc5-6 fail on GCC 3.x (for at least
326 # x=1 and x=2.1). So I'm modifying print_addr_2 to accept a failure
327 # condition. FIXME: It would be nice if the failure condition were
328 # conditional on the compiler version, but I'm not sufficiently
329 # motivated. I did hardwire in the versions of char * and int *,
330 # which will give some compiler-specificity to the failure.
332 proc print_addr_2_kfail { name good bad bugid } {
336 set good_pattern [string_to_regexp $good]
337 set bad_pattern [string_to_regexp $bad]
339 gdb_test_multiple "print &$name" "print &$name" {
340 -re ".* = .* $hex <$good_pattern>\r\n$gdb_prompt $" {
343 -re ".* = .* $hex <$bad_pattern>\r\n$gdb_prompt $" {
344 kfail $bugid "print &$name"
350 # Simple interfaces to print_addr_2.
353 proc print_addr { name } {
354 regsub {\(void\)} $name {()} expected
355 if {[string first "::" $name] == -1} {
356 # C function -- must be qutoed
359 print_addr_2 "$name" $expected
363 # Test name demangling for operators.
365 # The '(' at the end of each regex input pattern is so that we match only
366 # the one we are looking for. I.E. "operator&" would match both
367 # "operator&(foo &)" and "operator&&(foo &)".
369 # gdb-gnats bug gdb/18:
370 # "gdb can't parse "info func operator*" or "info func operator\*".
371 # The star in "operator*" is interpreted as a regexp, but the "\*"
372 # in "operator\*" is not a legal operator.
375 proc test_lookup_operator_functions {} {
376 global dm_operator_comma
377 global dm_operator_char_star
378 global dm_type_char_star
379 global dm_operator_char_star_quoted
380 global dm_type_foo_ref
382 global dm_type_void_star
384 # operator* requires quoting so that GDB does not treat it as a regexp.
385 info_func "operator\\*(" "void foo::operator*($dm_type_foo_ref);"
386 info_func "operator%(" "void foo::operator%($dm_type_foo_ref);"
387 info_func "operator-(" "void foo::operator-($dm_type_foo_ref);"
388 info_func "operator>>(" "void foo::operator>>($dm_type_foo_ref);"
389 info_func "operator!=(" "void foo::operator!=($dm_type_foo_ref);"
390 info_func "operator>(" "void foo::operator>($dm_type_foo_ref);"
391 info_func "operator>=(" "void foo::operator>=($dm_type_foo_ref);"
392 info_func "operator|(" "void foo::operator|($dm_type_foo_ref);"
393 info_func "operator&&(" "void foo::operator&&($dm_type_foo_ref);"
394 info_func "operator!(" "void foo::operator!($dm_type_void);"
395 info_func "operator++(" "void foo::operator++(int);"
396 info_func "operator=(" "void foo::operator=($dm_type_foo_ref);"
397 info_func "operator+=(" "void foo::operator+=($dm_type_foo_ref);"
398 # operator*= requires quoting so that GDB does not treat it as a regexp.
399 info_func "operator\\*=(" "void foo::operator*=($dm_type_foo_ref);"
400 info_func "operator%=(" "void foo::operator%=($dm_type_foo_ref);"
401 info_func "operator>>=(" "void foo::operator>>=($dm_type_foo_ref);"
402 info_func "operator|=(" "void foo::operator|=($dm_type_foo_ref);"
403 info_func "operator$dm_operator_comma\(" \
404 "void foo::operator$dm_operator_comma\($dm_type_foo_ref);"
405 info_func "operator/(" "void foo::operator/($dm_type_foo_ref);"
406 info_func "operator+(" "void foo::operator+($dm_type_foo_ref);"
407 info_func "operator<<(" "void foo::operator<<($dm_type_foo_ref);"
408 info_func "operator==(" "void foo::operator==($dm_type_foo_ref);"
409 info_func "operator<(" "void foo::operator<($dm_type_foo_ref);"
410 info_func "operator<=(" "void foo::operator<=($dm_type_foo_ref);"
411 info_func "operator&(" "void foo::operator&($dm_type_foo_ref);"
412 info_func "operator^(" "void foo::operator^($dm_type_foo_ref);"
413 info_func "operator||(" "void foo::operator||($dm_type_foo_ref);"
414 info_func "operator~(" "void foo::operator~($dm_type_void);"
415 info_func "operator--(" "void foo::operator--(int);"
416 info_func "operator->(" "foo *foo::operator->($dm_type_void);"
417 info_func "operator-=(" "void foo::operator-=($dm_type_foo_ref);"
418 info_func "operator/=(" "void foo::operator/=($dm_type_foo_ref);"
419 info_func "operator<<=(" "void foo::operator<<=($dm_type_foo_ref);"
420 info_func "operator&=(" "void foo::operator&=($dm_type_foo_ref);"
421 info_func "operator^=(" "void foo::operator^=($dm_type_foo_ref);"
422 # operator->* requires quoting so that GDB does not treat it as a regexp.
423 info_func "operator->\\*(" "void foo::operator->*($dm_type_foo_ref);"
425 # operator[] needs double backslashes, so that a single backslash
426 # will be sent to GDB, preventing the square brackets from being
427 # evaluated as a regular expression.
428 info_func "operator\\\[\\\](" "void foo::operator\[\]($dm_type_foo_ref);"
430 # These are gnarly because they might end with 'static'.
431 set dm_type_void_star_regexp [string_to_regexp $dm_type_void_star]
432 info_func_regexp "operator new(" "void \\*foo::operator new\\(.*\\)(| static);"
433 info_func_regexp "operator delete(" "void foo::operator delete\\($dm_type_void_star_regexp\\)(| static);"
435 info_func "operator int(" "int foo::operator int($dm_type_void);"
436 info_func "operator()(" "void foo::operator()($dm_type_foo_ref);"
437 info_func "operator $dm_operator_char_star_quoted\(" \
438 "char *foo::operator $dm_operator_char_star\($dm_type_void);"
443 proc test_paddr_operator_functions {} {
445 global dm_operator_comma
446 global dm_type_char_star
447 global dm_type_foo_ref
448 global dm_type_long_star
449 global dm_type_unsigned_int
451 global dm_type_void_star
452 global dm_operator_char_star
454 print_addr "foo::operator*($dm_type_foo_ref)"
455 print_addr "foo::operator%($dm_type_foo_ref)"
456 print_addr "foo::operator-($dm_type_foo_ref)"
457 print_addr "foo::operator>>($dm_type_foo_ref)"
458 print_addr "foo::operator!=($dm_type_foo_ref)"
459 print_addr "foo::operator>($dm_type_foo_ref)"
460 print_addr "foo::operator>=($dm_type_foo_ref)"
461 print_addr "foo::operator|($dm_type_foo_ref)"
462 print_addr "foo::operator&&($dm_type_foo_ref)"
463 print_addr "foo::operator!($dm_type_void)"
464 print_addr "foo::operator++(int)"
465 print_addr "foo::operator=($dm_type_foo_ref)"
466 print_addr "foo::operator+=($dm_type_foo_ref)"
467 print_addr "foo::operator*=($dm_type_foo_ref)"
468 print_addr "foo::operator%=($dm_type_foo_ref)"
469 print_addr "foo::operator>>=($dm_type_foo_ref)"
470 print_addr "foo::operator|=($dm_type_foo_ref)"
471 print_addr "foo::operator$dm_operator_comma\($dm_type_foo_ref)"
472 print_addr "foo::operator/($dm_type_foo_ref)"
473 print_addr "foo::operator+($dm_type_foo_ref)"
474 print_addr "foo::operator<<($dm_type_foo_ref)"
475 print_addr "foo::operator==($dm_type_foo_ref)"
476 print_addr "foo::operator<($dm_type_foo_ref)"
477 print_addr "foo::operator<=($dm_type_foo_ref)"
478 print_addr "foo::operator&($dm_type_foo_ref)"
479 print_addr "foo::operator^($dm_type_foo_ref)"
480 print_addr "foo::operator||($dm_type_foo_ref)"
481 print_addr "foo::operator~($dm_type_void)"
482 print_addr "foo::operator--(int)"
483 print_addr "foo::operator->($dm_type_void)"
484 print_addr "foo::operator-=($dm_type_foo_ref)"
485 print_addr "foo::operator/=($dm_type_foo_ref)"
486 print_addr "foo::operator<<=($dm_type_foo_ref)"
487 print_addr "foo::operator&=($dm_type_foo_ref)"
488 print_addr "foo::operator^=($dm_type_foo_ref)"
489 print_addr "foo::operator->*($dm_type_foo_ref)"
490 print_addr "foo::operator\[\]($dm_type_foo_ref)"
491 print_addr "foo::operator()($dm_type_foo_ref)"
493 gdb_test "print &foo::operator new" \
494 " = .* $hex <foo::operator new\\(.*\\)(| static)>"
495 gdb_test "print &foo::operator new\[\]" \
496 " = .* $hex <foo::operator new\\\[\\\]\\(.*\\)(| static)>"
498 print_addr "foo::operator delete($dm_type_void_star)"
499 print_addr "foo::operator delete\[\]($dm_type_void_star)"
501 print_addr "foo::operator int($dm_type_void)"
502 print_addr "foo::operator $dm_operator_char_star\($dm_type_void)"
506 # Test overloaded functions (1 arg).
509 proc test_paddr_overloaded_functions {} {
510 global dm_type_unsigned_int
513 global dm_type_unsigned_short
515 global dm_type_unsigned_long
517 print_addr "overload1arg($dm_type_void)"
518 print_addr "overload1arg(char)"
519 print_addr "overload1arg(signed char)"
520 print_addr "overload1arg(unsigned char)"
521 print_addr "overload1arg($dm_type_short)"
522 print_addr "overload1arg($dm_type_unsigned_short)"
523 print_addr "overload1arg(int)"
524 print_addr "overload1arg($dm_type_unsigned_int)"
525 print_addr "overload1arg($dm_type_long)"
526 print_addr "overload1arg($dm_type_unsigned_long)"
527 print_addr "overload1arg(float)"
528 print_addr "overload1arg(double)"
530 print_addr "overloadargs(int)"
531 print_addr "overloadargs(int, int)"
532 print_addr "overloadargs(int, int, int)"
533 print_addr "overloadargs(int, int, int, int)"
534 print_addr "overloadargs(int, int, int, int, int)"
535 print_addr "overloadargs(int, int, int, int, int, int)"
536 print_addr "overloadargs(int, int, int, int, int, int, int)"
537 print_addr "overloadargs(int, int, int, int, int, int, int, int)"
538 print_addr "overloadargs(int, int, int, int, int, int, int, int, int)"
539 print_addr "overloadargs(int, int, int, int, int, int, int, int, int, int)"
540 print_addr "overloadargs(int, int, int, int, int, int, int, int, int, int, int)"
543 proc test_paddr_hairy_functions {} {
546 global dm_type_char_star
547 global dm_type_int_star
548 global dm_type_long_star
549 global dm_type_typedef
551 print_addr_2 "hairyfunc1" "hairyfunc1(int)"
553 if {$dm_type_typedef == 0} {
554 print_addr_2 "hairyfunc2" "hairyfunc2(int (*)($dm_type_char_star))"
555 print_addr_2 "hairyfunc3" "hairyfunc3(int (*)(short (*)($dm_type_long_star)))"
556 print_addr_2 "hairyfunc4" "hairyfunc4(int (*)(short (*)($dm_type_char_star)))"
558 # gdb-gnats bug gdb/19:
559 # "gdb v3 demangler fails on hairyfunc5 hairyfunc6 hairyfunc7"
560 print_addr_2_kfail "hairyfunc5" "hairyfunc5(int (*(*)($dm_type_char_star))(long))" "hairyfunc5(int (*)(long) (*)(char*))" "gdb/19"
561 print_addr_2_kfail "hairyfunc6" "hairyfunc6(int (*(*)($dm_type_int_star))(long))" "hairyfunc6(int (*)(long) (*)(int*))" "gdb/19"
562 print_addr_2_kfail "hairyfunc7" "hairyfunc7(int (*(*)(int (*)($dm_type_char_star)))(long))" "hairyfunc7(int (*)(long) (*)(int (*)(char*)))" "gdb/19"
564 print_addr_2 "hairyfunc2" "hairyfunc2(PFPc_i)"
565 print_addr_2 "hairyfunc3" "hairyfunc3(PFPFPl_s_i)"
566 print_addr_2 "hairyfunc4" "hairyfunc4(PFPFPc_s_i)"
568 # gdb-gnats bug gdb/19:
569 # "gdb v3 demangler fails on hairyfunc5 hairyfunc6 hairyfunc7"
570 print_addr_2 "hairyfunc5" "hairyfunc5(PFPc_PFl_i)"
571 print_addr_2 "hairyfunc6" "hairyfunc6(PFPi_PFl_i)"
572 print_addr_2 "hairyfunc7" "hairyfunc7(PFPFPc_i_PFl_i)"
578 global dm_type_int_star
580 gdb_test_no_output "set width 0"
584 gdb_test_no_output "set language c++"
586 test_paddr_overloaded_functions
587 test_paddr_operator_functions
588 test_paddr_hairy_functions
589 test_lookup_operator_functions
591 # A regression test on errors involving operators
592 gdb_test "list foo::operator $dm_type_int_star" \
593 "Function \"foo::operator [string_to_regexp $dm_type_int_star]\" not defined\\."