1 # Copyright 1992, 1997, 1999, 2001-2004, 2007-2012 Free Software
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 # This file was written by Fred Fish. (fnf@cygnus.com)
18 # Adapted for g++ 3.0 ABI by Michael Chastain. (chastain@redhat.com)
20 if { [skip_cplus_tests] } { continue }
24 if { [get_compiler_info "c++"] } {
28 if {[prepare_for_testing $testfile.exp $testfile $srcfile {debug c++}]} {
33 # g++ changed its ABI between 2.95 and 3.0. gdb has two demanglers
34 # for the two different styles. The two demanglers have some subtle
35 # discrepancies in their output.
37 # old demangler new demangler
38 # --- --------- --- ---------
39 # "operator, " "operator,"
45 # "unsigned int" "unsigned"
48 # I probe for the forms in use.
49 # The defaults are for the v3 demangler (as of 2001-02-13).
52 set dm_operator_comma ","
53 set dm_type_char_star "char*"
54 set dm_type_char_star_quoted "char\\*"
55 set dm_type_foo_ref "foo&"
56 set dm_type_int_star "int*"
57 set dm_type_long_star "long*"
58 set dm_type_unsigned_int "unsigned"
59 set dm_type_void "void"
60 set dm_type_void_star "void*"
62 # Some other vagaries of GDB's type printing machinery. The integer types
63 # may have unsigned before or after their length, and may have "int"
64 # appended. The char* conversion operator may have name "char*" even if
65 # the type is "char *", because the name comes from the debug information
66 # and the type from GDB. Function types may not see through typedefs.
68 set dm_type_short "short"
69 set dm_type_long "long"
70 set dm_type_unsigned_short "unsigned short"
71 set dm_type_unsigned_long "unsigned long"
72 set dm_operator_char_star "char*"
73 set dm_operator_char_star_quoted "char\\*"
76 proc probe_demangler { } {
78 global dm_operator_comma
79 global dm_operator_char_star
80 global dm_operator_char_star_quoted
81 global dm_type_char_star
82 global dm_type_char_star_quoted
83 global dm_type_foo_ref
84 global dm_type_int_star
85 global dm_type_long_star
86 global dm_type_unsigned_int
88 global dm_type_void_star
90 global dm_type_unsigned_short
92 global dm_type_unsigned_long
93 global dm_type_typedef
95 gdb_test_multiple "print &foo::operator,(foo&)" \
96 "detect dm_operator_comma" {
97 -re ".*foo::operator, \\(.*foo.*&.*\\).*\r\n$gdb_prompt $" {
99 set dm_operator_comma ", "
100 pass "detect dm_operator_comma"
102 -re ".*foo::operator,\\(.*foo.*&.*\\).*\r\n$gdb_prompt $" {
104 pass "detect dm_operator_comma"
108 gdb_test_multiple "print &foo::operator char*($dm_type_void)" \
109 "detect dm_operator_char_star" {
110 -re ".*foo::operator char \\*\\(void\\).*\r\n$gdb_prompt $" {
111 # v2 demangler or GDB type printer
112 set dm_operator_char_star "char *"
113 set dm_operator_char_star_quoted "char \\*"
114 pass "detect dm_operator_char_star"
116 -re ".*foo::operator char\\*\\(\\).*\r\n$gdb_prompt $" {
118 pass "detect dm_operator_char_star"
122 gdb_test_multiple "print &dm_type_char_star" \
123 "detect dm_type_char_star" {
124 -re ".*dm_type_char_star\\(char \\*\\).*\r\n$gdb_prompt $" {
126 set dm_type_char_star "char *"
127 set dm_type_char_star_quoted "char \\*"
128 pass "detect dm_type_char_star"
130 -re ".*dm_type_char_star\\(char\\*\\).*\r\n$gdb_prompt $" {
132 pass "detect dm_type_char_star"
136 gdb_test_multiple "print &dm_type_foo_ref" \
137 "detect dm_type_foo_ref" {
138 -re ".*dm_type_foo_ref\\(foo &\\).*\r\n$gdb_prompt $" {
140 set dm_type_foo_ref "foo &"
141 pass "detect dm_type_foo_ref"
143 -re ".*dm_type_foo_ref\\(foo&\\).*\r\n$gdb_prompt $" {
145 pass "detect dm_type_foo_ref"
149 gdb_test_multiple "print &dm_type_int_star" \
150 "detect dm_type_int_star" {
151 -re ".*dm_type_int_star\\(int \\*\\).*\r\n$gdb_prompt $" {
153 set dm_type_int_star "int *"
154 pass "detect dm_type_int_star"
156 -re ".*dm_type_int_star\\(int\\*\\).*\r\n$gdb_prompt $" {
158 pass "detect dm_type_int_star"
162 gdb_test_multiple "print &dm_type_long_star" \
163 "detect dm_type_long_star" {
164 -re ".*dm_type_long_star\\(long \\*\\).*\r\n$gdb_prompt $" {
166 set dm_type_long_star "long *"
167 pass "detect dm_type_long_star"
169 -re ".*dm_type_long_star\\(long\\*\\).*\r\n$gdb_prompt $" {
171 pass "detect dm_type_long_star"
173 -re ".*dm_type_long_star\\(long int \\*\\).*\r\n$gdb_prompt $" {
174 # GCC v3 and GDB's type printer
175 set dm_type_long_star "long int *"
176 pass "detect dm_type_long_star"
180 gdb_test_multiple "print &dm_type_unsigned_int" \
181 "detect dm_type_unsigned_int" {
182 -re ".*dm_type_unsigned_int\\(unsigned int\\).*\r\n$gdb_prompt $" {
184 set dm_type_unsigned_int "unsigned int"
185 pass "detect dm_type_unsigned_int"
187 -re ".*dm_type_unsigned_int\\(unsigned\\).*\r\n$gdb_prompt $" {
189 pass "detect dm_type_unsigned_int"
193 gdb_test_multiple "print &dm_type_void" \
194 "detect dm_type_void" {
195 -re ".*dm_type_void\\(void\\).*\r\n$gdb_prompt $" {
197 set dm_type_void "void"
198 pass "detect dm_type_void"
200 -re ".*dm_type_void\\(\\).*\r\n$gdb_prompt $" {
202 pass "detect dm_type_void"
206 gdb_test_multiple "print &dm_type_void_star" \
207 "detect dm_type_void_star" {
208 -re ".*dm_type_void_star\\(void \\*\\).*\r\n$gdb_prompt $" {
210 set dm_type_void_star "void *"
211 pass "detect dm_type_void_star"
213 -re ".*dm_type_void_star\\(void\\*\\).*\r\n$gdb_prompt $" {
215 pass "detect dm_type_void_star"
219 gdb_test_multiple "print &dm_type_short" \
220 "detect dm_type_short" {
221 -re ".*dm_type_short\\(short\\).*\r\n$gdb_prompt $" {
222 # v2 and v3 demanglers
223 pass "detect dm_type_short"
225 -re ".*dm_type_short\\(short int\\).*\r\n$gdb_prompt $" {
227 set dm_type_short "short int"
228 pass "detect dm_type_short"
232 gdb_test_multiple "print &dm_type_unsigned_short" \
233 "detect dm_type_unsigned_short" {
234 -re ".*dm_type_unsigned_short\\(unsigned short\\).*\r\n$gdb_prompt $" {
235 # v2 and v3 demanglers
236 pass "detect dm_type_unsigned_short"
238 -re ".*dm_type_unsigned_short\\(short unsigned int\\).*\r\n$gdb_prompt $" {
240 set dm_type_unsigned_short "short unsigned int"
241 pass "detect dm_type_unsigned_short"
245 gdb_test_multiple "print &dm_type_long" \
246 "detect dm_type_long" {
247 -re ".*dm_type_long\\(long\\).*\r\n$gdb_prompt $" {
248 # v2 and v3 demanglers
249 pass "detect dm_type_long"
251 -re ".*dm_type_long\\(long int\\).*\r\n$gdb_prompt $" {
253 set dm_type_long "long int"
254 pass "detect dm_type_long"
258 gdb_test_multiple "print &dm_type_unsigned_long" \
259 "detect dm_type_unsigned_long" {
260 -re ".*dm_type_unsigned_long\\(unsigned long\\).*\r\n$gdb_prompt $" {
261 # v2 and v3 demanglers
262 pass "detect dm_type_unsigned_long"
264 -re ".*dm_type_unsigned_long\\(long unsigned int\\).*\r\n$gdb_prompt $" {
266 set dm_type_unsigned_long "long unsigned int"
267 pass "detect dm_type_unsigned_long"
271 gdb_test_multiple "print &dm_type_typedef" \
272 "detect dm_type_typedef" {
273 -re ".*dm_type_typedef\\(int\\).*\r\n$gdb_prompt $" {
274 # v2 and v3 demanglers
275 pass "detect dm_type_typedef"
277 -re ".*dm_type_typedef\\(myint\\).*\r\n$gdb_prompt $" {
279 set dm_type_typedef 1
280 pass "detect dm_type_typedef"
286 # Lookup a specific C++ function and print the demangled type.
287 # This form accepts the demangled type as a regexp.
290 proc info_func_regexp { name demangled } {
293 regsub {\\\(void\\\)} $demangled {\(\)} demangled
295 gdb_test "info function $name" \
296 "File .*:\r\n(class|)${demangled}.*" \
297 "info function for \"$name\""
301 # Lookup a specific C++ function and print the demangled type.
302 # This form accepts the demangled type as a literal string.
305 proc info_func { name demangled } {
306 info_func_regexp "$name" [string_to_regexp "$demangled"]
310 # Print the address of a function.
311 # This checks that I can lookup a fully qualified C++ function.
312 # This also checks the argument types on the return string.
314 # Note: carlton/2003-01-16: If you modify this, make a corresponding
315 # modification to print_addr_2_kfail.
317 proc print_addr_2 { name good } {
321 set good_pattern [string_to_regexp $good]
323 gdb_test "print &$name" \
324 ".* = .* $hex <$good_pattern>"
327 # NOTE: carlton/2003-01-16: hairyfunc5-6 fail on GCC 3.x (for at least
328 # x=1 and x=2.1). So I'm modifying print_addr_2 to accept a failure
329 # condition. FIXME: It would be nice if the failure condition were
330 # conditional on the compiler version, but I'm not sufficiently
331 # motivated. I did hardwire in the versions of char * and int *,
332 # which will give some compiler-specificity to the failure.
334 proc print_addr_2_kfail { name good bad bugid } {
338 set good_pattern [string_to_regexp $good]
339 set bad_pattern [string_to_regexp $bad]
341 gdb_test_multiple "print &$name" "print &$name" {
342 -re ".* = .* $hex <$good_pattern>\r\n$gdb_prompt $" {
345 -re ".* = .* $hex <$bad_pattern>\r\n$gdb_prompt $" {
346 kfail $bugid "print &$name"
352 # Simple interfaces to print_addr_2.
355 proc print_addr { name } {
356 regsub {\(void\)} $name {()} expected
357 if {[string first "::" $name] == -1} {
358 # C function -- must be qutoed
361 print_addr_2 "$name" $expected
365 # Test name demangling for operators.
367 # The '(' at the end of each regex input pattern is so that we match only
368 # the one we are looking for. I.E. "operator&" would match both
369 # "operator&(foo &)" and "operator&&(foo &)".
371 # gdb-gnats bug gdb/18:
372 # "gdb can't parse "info func operator*" or "info func operator\*".
373 # The star in "operator*" is interpreted as a regexp, but the "\*"
374 # in "operator\*" is not a legal operator.
377 proc test_lookup_operator_functions {} {
378 global dm_operator_comma
379 global dm_operator_char_star
380 global dm_type_char_star
381 global dm_operator_char_star_quoted
382 global dm_type_foo_ref
384 global dm_type_void_star
386 # operator* requires quoting so that GDB does not treat it as a regexp.
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_foo_ref);"
396 info_func "operator!(" "void foo::operator!($dm_type_void);"
397 info_func "operator++(" "void foo::operator++(int);"
398 info_func "operator=(" "void foo::operator=($dm_type_foo_ref);"
399 info_func "operator+=(" "void foo::operator+=($dm_type_foo_ref);"
400 # operator*= requires quoting so that GDB does not treat it as a regexp.
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|=(" "void foo::operator|=($dm_type_foo_ref);"
405 info_func "operator$dm_operator_comma\(" \
406 "void foo::operator$dm_operator_comma\($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_foo_ref);"
416 info_func "operator~(" "void foo::operator~($dm_type_void);"
417 info_func "operator--(" "void foo::operator--(int);"
418 info_func "operator->(" "foo *foo::operator->($dm_type_void);"
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 info_func "operator^=(" "void foo::operator^=($dm_type_foo_ref);"
424 # operator->* requires quoting so that GDB does not treat it as a regexp.
425 info_func "operator->\\*(" "void foo::operator->*($dm_type_foo_ref);"
427 # operator[] needs double backslashes, so that a single backslash
428 # will be sent to GDB, preventing the square brackets from being
429 # evaluated as a regular expression.
430 info_func "operator\\\[\\\](" "void foo::operator\[\]($dm_type_foo_ref);"
432 # These are gnarly because they might end with 'static'.
433 set dm_type_void_star_regexp [string_to_regexp $dm_type_void_star]
434 info_func_regexp "operator new(" "void \\*foo::operator new\\(.*\\)(| static);"
435 info_func_regexp "operator delete(" "void foo::operator delete\\($dm_type_void_star_regexp\\)(| static);"
437 info_func "operator int(" "int foo::operator int($dm_type_void);"
438 info_func "operator()(" "void foo::operator()($dm_type_foo_ref);"
439 info_func "operator $dm_operator_char_star_quoted\(" \
440 "char *foo::operator $dm_operator_char_star\($dm_type_void);"
445 proc test_paddr_operator_functions {} {
447 global hp_aCC_compiler
448 global dm_operator_comma
449 global dm_type_char_star
450 global dm_type_foo_ref
451 global dm_type_long_star
452 global dm_type_unsigned_int
454 global dm_type_void_star
455 global dm_operator_char_star
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_foo_ref)"
465 print_addr "foo::operator&&($dm_type_foo_ref)"
466 print_addr "foo::operator!($dm_type_void)"
467 print_addr "foo::operator++(int)"
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_type_foo_ref)"
473 print_addr "foo::operator|=($dm_type_foo_ref)"
474 print_addr "foo::operator$dm_operator_comma\($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_foo_ref)"
483 print_addr "foo::operator||($dm_type_foo_ref)"
484 print_addr "foo::operator~($dm_type_void)"
485 print_addr "foo::operator--(int)"
486 print_addr "foo::operator->($dm_type_void)"
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)"
493 print_addr "foo::operator\[\]($dm_type_foo_ref)"
494 print_addr "foo::operator()($dm_type_foo_ref)"
496 gdb_test "print &foo::operator new" \
497 " = .* $hex <foo::operator new\\(.*\\)(| static)>"
498 gdb_test "print &foo::operator new\[\]" \
499 " = .* $hex <foo::operator new\\\[\\\]\\(.*\\)(| static)>"
500 if { !$hp_aCC_compiler } {
501 print_addr "foo::operator delete($dm_type_void_star)"
502 print_addr "foo::operator delete[]($dm_type_void_star)"
504 gdb_test "print &'foo::operator delete($dm_type_void_star) static'" \
505 " = .*(0x\[0-9a-f\]+|) <foo::operator delete.*>"
508 print_addr "foo::operator int($dm_type_void)"
509 print_addr "foo::operator $dm_operator_char_star\($dm_type_void)"
513 # Test overloaded functions (1 arg).
516 proc test_paddr_overloaded_functions {} {
517 global dm_type_unsigned_int
520 global dm_type_unsigned_short
522 global dm_type_unsigned_long
524 print_addr "overload1arg($dm_type_void)"
525 print_addr "overload1arg(char)"
526 print_addr "overload1arg(signed char)"
527 print_addr "overload1arg(unsigned char)"
528 print_addr "overload1arg($dm_type_short)"
529 print_addr "overload1arg($dm_type_unsigned_short)"
530 print_addr "overload1arg(int)"
531 print_addr "overload1arg($dm_type_unsigned_int)"
532 print_addr "overload1arg($dm_type_long)"
533 print_addr "overload1arg($dm_type_unsigned_long)"
534 print_addr "overload1arg(float)"
535 print_addr "overload1arg(double)"
537 print_addr "overloadargs(int)"
538 print_addr "overloadargs(int, int)"
539 print_addr "overloadargs(int, int, int)"
540 print_addr "overloadargs(int, int, int, int)"
541 print_addr "overloadargs(int, int, int, int, int)"
542 print_addr "overloadargs(int, int, int, int, int, int)"
543 print_addr "overloadargs(int, int, int, int, int, int, int)"
544 print_addr "overloadargs(int, int, int, int, int, int, int, int)"
545 print_addr "overloadargs(int, int, int, int, int, int, int, int, int)"
546 print_addr "overloadargs(int, int, int, int, int, int, int, int, int, int)"
547 print_addr "overloadargs(int, int, int, int, int, int, int, int, int, int, int)"
550 proc test_paddr_hairy_functions {} {
553 global dm_type_char_star
554 global dm_type_int_star
555 global dm_type_long_star
556 global dm_type_typedef
558 print_addr_2 "hairyfunc1" "hairyfunc1(int)"
560 if {$dm_type_typedef == 0} {
561 print_addr_2 "hairyfunc2" "hairyfunc2(int (*)($dm_type_char_star))"
562 print_addr_2 "hairyfunc3" "hairyfunc3(int (*)(short (*)($dm_type_long_star)))"
563 print_addr_2 "hairyfunc4" "hairyfunc4(int (*)(short (*)($dm_type_char_star)))"
565 # gdb-gnats bug gdb/19:
566 # "gdb v3 demangler fails on hairyfunc5 hairyfunc6 hairyfunc7"
567 print_addr_2_kfail "hairyfunc5" "hairyfunc5(int (*(*)($dm_type_char_star))(long))" "hairyfunc5(int (*)(long) (*)(char*))" "gdb/19"
568 print_addr_2_kfail "hairyfunc6" "hairyfunc6(int (*(*)($dm_type_int_star))(long))" "hairyfunc6(int (*)(long) (*)(int*))" "gdb/19"
569 print_addr_2_kfail "hairyfunc7" "hairyfunc7(int (*(*)(int (*)($dm_type_char_star)))(long))" "hairyfunc7(int (*)(long) (*)(int (*)(char*)))" "gdb/19"
571 print_addr_2 "hairyfunc2" "hairyfunc2(PFPc_i)"
572 print_addr_2 "hairyfunc3" "hairyfunc3(PFPFPl_s_i)"
573 print_addr_2 "hairyfunc4" "hairyfunc4(PFPFPc_s_i)"
575 # gdb-gnats bug gdb/19:
576 # "gdb v3 demangler fails on hairyfunc5 hairyfunc6 hairyfunc7"
577 print_addr_2 "hairyfunc5" "hairyfunc5(PFPc_PFl_i)"
578 print_addr_2 "hairyfunc6" "hairyfunc6(PFPi_PFl_i)"
579 print_addr_2 "hairyfunc7" "hairyfunc7(PFPFPc_i_PFl_i)"
585 global dm_type_int_star
587 clean_restart $binfile
589 gdb_test_no_output "set width 0"
593 gdb_test_no_output "set language c++"
595 test_paddr_overloaded_functions
596 test_paddr_operator_functions
597 test_paddr_hairy_functions
598 test_lookup_operator_functions
600 # A regression test on errors involving operators
601 gdb_test "list foo::operator $dm_type_int_star" \
602 "Function \"foo::operator [string_to_regexp $dm_type_int_star]\" not defined\\."