1 # Copyright 1992-2019 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 if { [skip_cplus_tests] } { continue }
23 if { [get_compiler_info "c++"] } {
27 if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug c++}]} {
32 # g++ changed its ABI between 2.95 and 3.0. gdb has two demanglers
33 # for the two different styles. The two demanglers have some subtle
34 # discrepancies in their output.
36 # old demangler new demangler
37 # --- --------- --- ---------
38 # "operator, " "operator,"
44 # "unsigned int" "unsigned"
47 # I probe for the forms in use.
48 # The defaults are for the v3 demangler (as of 2001-02-13).
51 set dm_operator_comma ","
52 set dm_type_char_star "char*"
53 set dm_type_char_star_quoted "char\\*"
54 set dm_type_foo_ref "foo&"
55 set dm_type_int_star "int*"
56 set dm_type_long_star "long*"
57 set dm_type_unsigned_int "unsigned"
58 set dm_type_void "void"
59 set dm_type_void_star "void*"
61 # Some other vagaries of GDB's type printing machinery. The integer types
62 # may have unsigned before or after their length, and may have "int"
63 # appended. The char* conversion operator may have name "char*" even if
64 # the type is "char *", because the name comes from the debug information
65 # and the type from GDB. Function types may not see through typedefs.
67 set dm_type_short "short"
68 set dm_type_long "long"
69 set dm_type_unsigned_short "unsigned short"
70 set dm_type_unsigned_long "unsigned long"
71 set dm_operator_char_star "char*"
72 set dm_operator_char_star_quoted "char\\*"
75 proc probe_demangler { } {
77 global dm_operator_comma
78 global dm_operator_char_star
79 global dm_operator_char_star_quoted
80 global dm_type_char_star
81 global dm_type_char_star_quoted
82 global dm_type_foo_ref
83 global dm_type_int_star
84 global dm_type_long_star
85 global dm_type_unsigned_int
87 global dm_type_void_star
89 global dm_type_unsigned_short
91 global dm_type_unsigned_long
92 global dm_type_typedef
94 gdb_test_multiple "print &foo::operator,(foo&)" \
95 "detect dm_operator_comma" {
96 -re ".*foo::operator, \\(.*foo.*&.*\\).*\r\n$gdb_prompt $" {
98 set dm_operator_comma ", "
99 pass "detect dm_operator_comma"
101 -re ".*foo::operator,\\(.*foo.*&.*\\).*\r\n$gdb_prompt $" {
103 pass "detect dm_operator_comma"
107 gdb_test_multiple "print &foo::operator char*($dm_type_void)" \
108 "detect dm_operator_char_star" {
109 -re ".*foo::operator char \\*\\(void\\).*\r\n$gdb_prompt $" {
110 # v2 demangler or GDB type printer
111 set dm_operator_char_star "char *"
112 set dm_operator_char_star_quoted "char \\*"
113 pass "detect dm_operator_char_star"
115 -re ".*foo::operator char\\*\\(\\).*\r\n$gdb_prompt $" {
117 pass "detect dm_operator_char_star"
121 gdb_test_multiple "print &dm_type_char_star" \
122 "detect dm_type_char_star" {
123 -re ".*dm_type_char_star\\(char \\*\\).*\r\n$gdb_prompt $" {
125 set dm_type_char_star "char *"
126 set dm_type_char_star_quoted "char \\*"
127 pass "detect dm_type_char_star"
129 -re ".*dm_type_char_star\\(char\\*\\).*\r\n$gdb_prompt $" {
131 pass "detect dm_type_char_star"
135 gdb_test_multiple "print &dm_type_foo_ref" \
136 "detect dm_type_foo_ref" {
137 -re ".*dm_type_foo_ref\\(foo &\\).*\r\n$gdb_prompt $" {
139 set dm_type_foo_ref "foo &"
140 pass "detect dm_type_foo_ref"
142 -re ".*dm_type_foo_ref\\(foo&\\).*\r\n$gdb_prompt $" {
144 pass "detect dm_type_foo_ref"
148 gdb_test_multiple "print &dm_type_int_star" \
149 "detect dm_type_int_star" {
150 -re ".*dm_type_int_star\\(int \\*\\).*\r\n$gdb_prompt $" {
152 set dm_type_int_star "int *"
153 pass "detect dm_type_int_star"
155 -re ".*dm_type_int_star\\(int\\*\\).*\r\n$gdb_prompt $" {
157 pass "detect dm_type_int_star"
161 gdb_test_multiple "print &dm_type_long_star" \
162 "detect dm_type_long_star" {
163 -re ".*dm_type_long_star\\(long \\*\\).*\r\n$gdb_prompt $" {
165 set dm_type_long_star "long *"
166 pass "detect dm_type_long_star"
168 -re ".*dm_type_long_star\\(long\\*\\).*\r\n$gdb_prompt $" {
170 pass "detect dm_type_long_star"
172 -re ".*dm_type_long_star\\(long int \\*\\).*\r\n$gdb_prompt $" {
173 # GCC v3 and GDB's type printer
174 set dm_type_long_star "long int *"
175 pass "detect dm_type_long_star"
179 gdb_test_multiple "print &dm_type_unsigned_int" \
180 "detect dm_type_unsigned_int" {
181 -re ".*dm_type_unsigned_int\\(unsigned int\\).*\r\n$gdb_prompt $" {
183 set dm_type_unsigned_int "unsigned int"
184 pass "detect dm_type_unsigned_int"
186 -re ".*dm_type_unsigned_int\\(unsigned\\).*\r\n$gdb_prompt $" {
188 pass "detect dm_type_unsigned_int"
192 gdb_test_multiple "print &dm_type_void" \
193 "detect dm_type_void" {
194 -re ".*dm_type_void\\(void\\).*\r\n$gdb_prompt $" {
196 set dm_type_void "void"
197 pass "detect dm_type_void"
199 -re ".*dm_type_void\\(\\).*\r\n$gdb_prompt $" {
201 pass "detect dm_type_void"
205 gdb_test_multiple "print &dm_type_void_star" \
206 "detect dm_type_void_star" {
207 -re ".*dm_type_void_star\\(void \\*\\).*\r\n$gdb_prompt $" {
209 set dm_type_void_star "void *"
210 pass "detect dm_type_void_star"
212 -re ".*dm_type_void_star\\(void\\*\\).*\r\n$gdb_prompt $" {
214 pass "detect dm_type_void_star"
218 gdb_test_multiple "print &dm_type_short" \
219 "detect dm_type_short" {
220 -re ".*dm_type_short\\(short\\).*\r\n$gdb_prompt $" {
221 # v2 and v3 demanglers
222 pass "detect dm_type_short"
224 -re ".*dm_type_short\\(short int\\).*\r\n$gdb_prompt $" {
226 set dm_type_short "short int"
227 pass "detect dm_type_short"
231 gdb_test_multiple "print &dm_type_unsigned_short" \
232 "detect dm_type_unsigned_short" {
233 -re ".*dm_type_unsigned_short\\(unsigned short\\).*\r\n$gdb_prompt $" {
234 # v2 and v3 demanglers
235 pass "detect dm_type_unsigned_short"
237 -re ".*dm_type_unsigned_short\\(short unsigned int\\).*\r\n$gdb_prompt $" {
239 set dm_type_unsigned_short "short unsigned int"
240 pass "detect dm_type_unsigned_short"
244 gdb_test_multiple "print &dm_type_long" \
245 "detect dm_type_long" {
246 -re ".*dm_type_long\\(long\\).*\r\n$gdb_prompt $" {
247 # v2 and v3 demanglers
248 pass "detect dm_type_long"
250 -re ".*dm_type_long\\(long int\\).*\r\n$gdb_prompt $" {
252 set dm_type_long "long int"
253 pass "detect dm_type_long"
257 gdb_test_multiple "print &dm_type_unsigned_long" \
258 "detect dm_type_unsigned_long" {
259 -re ".*dm_type_unsigned_long\\(unsigned long\\).*\r\n$gdb_prompt $" {
260 # v2 and v3 demanglers
261 pass "detect dm_type_unsigned_long"
263 -re ".*dm_type_unsigned_long\\(long unsigned int\\).*\r\n$gdb_prompt $" {
265 set dm_type_unsigned_long "long unsigned int"
266 pass "detect dm_type_unsigned_long"
270 gdb_test_multiple "print &dm_type_typedef" \
271 "detect dm_type_typedef" {
272 -re ".*dm_type_typedef\\(int\\).*\r\n$gdb_prompt $" {
273 # v2 and v3 demanglers
274 pass "detect dm_type_typedef"
276 -re ".*dm_type_typedef\\(myint\\).*\r\n$gdb_prompt $" {
278 set dm_type_typedef 1
279 pass "detect dm_type_typedef"
285 # Lookup a specific C++ function and print the demangled type.
286 # This form accepts the demangled type as a regexp.
289 proc info_func_regexp { name demangled } {
292 regsub {\\\(void\\\)} $demangled {\(\)} demangled
294 gdb_test "info function $name" \
295 "File .*:\t(class|)${demangled}.*" \
296 "info function for \"$name\""
300 # Lookup a specific C++ function and print the demangled type.
301 # This form accepts the demangled type as a literal string.
304 proc info_func { name demangled } {
305 info_func_regexp "$name" [string_to_regexp "$demangled"]
309 # Print the address of a function.
310 # This checks that I can lookup a fully qualified C++ function.
311 # This also checks the argument types on the return string.
313 # Note: carlton/2003-01-16: If you modify this, make a corresponding
314 # modification to print_addr_2_kfail.
316 proc print_addr_2 { name good } {
320 set good_pattern [string_to_regexp $good]
322 gdb_test "print &$name" \
323 ".* = .* $hex <$good_pattern>"
326 # NOTE: carlton/2003-01-16: hairyfunc5-6 fail on GCC 3.x (for at least
327 # x=1 and x=2.1). So I'm modifying print_addr_2 to accept a failure
328 # condition. FIXME: It would be nice if the failure condition were
329 # conditional on the compiler version, but I'm not sufficiently
330 # motivated. I did hardwire in the versions of char * and int *,
331 # which will give some compiler-specificity to the failure.
333 proc print_addr_2_kfail { name good bad bugid } {
337 set good_pattern [string_to_regexp $good]
338 set bad_pattern [string_to_regexp $bad]
340 gdb_test_multiple "print &$name" "print &$name" {
341 -re ".* = .* $hex <$good_pattern>\r\n$gdb_prompt $" {
344 -re ".* = .* $hex <$bad_pattern>\r\n$gdb_prompt $" {
345 kfail $bugid "print &$name"
351 # Simple interfaces to print_addr_2.
354 proc print_addr { name } {
355 regsub {\(void\)} $name {()} expected
356 if {[string first "::" $name] == -1} {
357 # C function -- must be qutoed
360 print_addr_2 "$name" $expected
364 # Test name demangling for operators.
366 # The '(' at the end of each regex input pattern is so that we match only
367 # the one we are looking for. I.E. "operator&" would match both
368 # "operator&(foo &)" and "operator&&(foo &)".
370 # gdb-gnats bug gdb/18:
371 # "gdb can't parse "info func operator*" or "info func operator\*".
372 # The star in "operator*" is interpreted as a regexp, but the "\*"
373 # in "operator\*" is not a legal operator.
376 proc test_lookup_operator_functions {} {
377 global dm_operator_comma
378 global dm_operator_char_star
379 global dm_type_char_star
380 global dm_operator_char_star_quoted
381 global dm_type_foo_ref
383 global dm_type_void_star
385 # operator* requires quoting so that GDB does not treat it as a regexp.
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_foo_ref);"
395 info_func "operator!(" "void foo::operator!($dm_type_void);"
396 info_func "operator++(" "void foo::operator++(int);"
397 info_func "operator=(" "void foo::operator=($dm_type_foo_ref);"
398 info_func "operator+=(" "void foo::operator+=($dm_type_foo_ref);"
399 # operator*= requires quoting so that GDB does not treat it as a regexp.
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|=(" "void foo::operator|=($dm_type_foo_ref);"
404 info_func "operator$dm_operator_comma\(" \
405 "void foo::operator$dm_operator_comma\($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_foo_ref);"
415 info_func "operator~(" "void foo::operator~($dm_type_void);"
416 info_func "operator--(" "void foo::operator--(int);"
417 info_func "operator->(" "foo *foo::operator->($dm_type_void);"
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 info_func "operator^=(" "void foo::operator^=($dm_type_foo_ref);"
423 # operator->* requires quoting so that GDB does not treat it as a regexp.
424 info_func "operator->\\*(" "void foo::operator->*($dm_type_foo_ref);"
426 # operator[] needs double backslashes, so that a single backslash
427 # will be sent to GDB, preventing the square brackets from being
428 # evaluated as a regular expression.
429 info_func "operator\\\[\\\](" "void foo::operator\[\]($dm_type_foo_ref);"
431 # These are gnarly because they might end with 'static'.
432 set dm_type_void_star_regexp [string_to_regexp $dm_type_void_star]
433 info_func_regexp "operator new(" "void \\*foo::operator new\\(.*\\)(| static);"
434 info_func_regexp "operator delete(" "void foo::operator delete\\($dm_type_void_star_regexp\\)(| static);"
436 info_func "operator int(" "int foo::operator int($dm_type_void);"
437 info_func "operator()(" "void foo::operator()($dm_type_foo_ref);"
438 info_func "operator $dm_operator_char_star_quoted\(" \
439 "char *foo::operator $dm_operator_char_star\($dm_type_void);"
444 proc test_paddr_operator_functions {} {
446 global dm_operator_comma
447 global dm_type_char_star
448 global dm_type_foo_ref
449 global dm_type_long_star
450 global dm_type_unsigned_int
452 global dm_type_void_star
453 global dm_operator_char_star
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_foo_ref)"
464 print_addr "foo::operator!($dm_type_void)"
465 print_addr "foo::operator++(int)"
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_type_foo_ref)"
472 print_addr "foo::operator$dm_operator_comma\($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_foo_ref)"
482 print_addr "foo::operator~($dm_type_void)"
483 print_addr "foo::operator--(int)"
484 print_addr "foo::operator->($dm_type_void)"
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)"
492 print_addr "foo::operator()($dm_type_foo_ref)"
494 gdb_test "print &foo::operator new" \
495 " = .* $hex <foo::operator new\\(.*\\)(| static)>"
496 gdb_test "print &foo::operator new\[\]" \
497 " = .* $hex <foo::operator new\\\[\\\]\\(.*\\)(| static)>"
499 print_addr "foo::operator delete($dm_type_void_star)"
500 print_addr "foo::operator delete[]($dm_type_void_star)"
502 print_addr "foo::operator int($dm_type_void)"
503 print_addr "foo::operator $dm_operator_char_star\($dm_type_void)"
507 # Test overloaded functions (1 arg).
510 proc test_paddr_overloaded_functions {} {
511 global dm_type_unsigned_int
514 global dm_type_unsigned_short
516 global dm_type_unsigned_long
518 print_addr "overload1arg($dm_type_void)"
519 print_addr "overload1arg(char)"
520 print_addr "overload1arg(signed char)"
521 print_addr "overload1arg(unsigned char)"
522 print_addr "overload1arg($dm_type_short)"
523 print_addr "overload1arg($dm_type_unsigned_short)"
524 print_addr "overload1arg(int)"
525 print_addr "overload1arg($dm_type_unsigned_int)"
526 print_addr "overload1arg($dm_type_long)"
527 print_addr "overload1arg($dm_type_unsigned_long)"
528 print_addr "overload1arg(float)"
529 print_addr "overload1arg(double)"
531 print_addr "overloadargs(int)"
532 print_addr "overloadargs(int, int)"
533 print_addr "overloadargs(int, int, int)"
534 print_addr "overloadargs(int, int, int, int)"
535 print_addr "overloadargs(int, int, int, int, int)"
536 print_addr "overloadargs(int, int, int, int, int, int)"
537 print_addr "overloadargs(int, int, int, int, int, int, int)"
538 print_addr "overloadargs(int, int, int, int, int, int, int, int)"
539 print_addr "overloadargs(int, int, int, int, int, int, int, int, int)"
540 print_addr "overloadargs(int, int, int, int, int, int, int, int, int, int)"
541 print_addr "overloadargs(int, int, int, int, int, int, int, int, int, int, int)"
544 proc test_paddr_hairy_functions {} {
547 global dm_type_char_star
548 global dm_type_int_star
549 global dm_type_long_star
550 global dm_type_typedef
552 print_addr_2 "hairyfunc1" "hairyfunc1(int)"
554 if {$dm_type_typedef == 0} {
555 print_addr_2 "hairyfunc2" "hairyfunc2(int (*)($dm_type_char_star))"
556 print_addr_2 "hairyfunc3" "hairyfunc3(int (*)(short (*)($dm_type_long_star)))"
557 print_addr_2 "hairyfunc4" "hairyfunc4(int (*)(short (*)($dm_type_char_star)))"
559 # gdb-gnats bug gdb/19:
560 # "gdb v3 demangler fails on hairyfunc5 hairyfunc6 hairyfunc7"
561 print_addr_2_kfail "hairyfunc5" "hairyfunc5(int (*(*)($dm_type_char_star))(long))" "hairyfunc5(int (*)(long) (*)(char*))" "gdb/19"
562 print_addr_2_kfail "hairyfunc6" "hairyfunc6(int (*(*)($dm_type_int_star))(long))" "hairyfunc6(int (*)(long) (*)(int*))" "gdb/19"
563 print_addr_2_kfail "hairyfunc7" "hairyfunc7(int (*(*)(int (*)($dm_type_char_star)))(long))" "hairyfunc7(int (*)(long) (*)(int (*)(char*)))" "gdb/19"
565 print_addr_2 "hairyfunc2" "hairyfunc2(PFPc_i)"
566 print_addr_2 "hairyfunc3" "hairyfunc3(PFPFPl_s_i)"
567 print_addr_2 "hairyfunc4" "hairyfunc4(PFPFPc_s_i)"
569 # gdb-gnats bug gdb/19:
570 # "gdb v3 demangler fails on hairyfunc5 hairyfunc6 hairyfunc7"
571 print_addr_2 "hairyfunc5" "hairyfunc5(PFPc_PFl_i)"
572 print_addr_2 "hairyfunc6" "hairyfunc6(PFPi_PFl_i)"
573 print_addr_2 "hairyfunc7" "hairyfunc7(PFPFPc_i_PFl_i)"
579 global dm_type_int_star
581 clean_restart $binfile
583 gdb_test_no_output "set width 0"
587 gdb_test_no_output "set language c++"
589 test_paddr_overloaded_functions
590 test_paddr_operator_functions
591 test_paddr_hairy_functions
592 test_lookup_operator_functions
594 # A regression test on errors involving operators
595 gdb_test "list foo::operator $dm_type_int_star" \
596 "Function \"foo::operator [string_to_regexp $dm_type_int_star]\" not defined\\."