1 # Copyright 1998, 1999, 2003, 2004, 2006, 2007 Free Software Foundation, Inc.
3 # This file is part of the gdb testsuite
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # Tests for pointer-to-member support
19 # Written by Satish Pai <pai@apollo.hp.com> 1997-08-19
20 # Rewritten by Michael Chastain <mec.gnu@mindspring.com> 2004-01-11
28 if { [skip_cplus_tests] } { continue }
33 set testfile "member-ptr"
34 set srcfile ${testfile}.cc
35 set binfile ${objdir}/${subdir}/${testfile}
37 if [get_compiler_info ${binfile} "c++"] {
41 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug c++}] != "" } {
42 untested member-ptr.exp
48 gdb_reinitialize_dir $srcdir/$subdir
51 if ![runto_main] then {
52 perror "couldn't run to breakpoint"
56 gdb_breakpoint [gdb_get_line_number "Breakpoint 1 here"]
57 gdb_continue_to_breakpoint "continue to pmi = NULL"
59 # ======================
60 # pointer to member data
61 # ======================
63 # ptype on pointer to data member
65 set name "ptype pmi (A::j)"
66 gdb_test_multiple "ptype pmi" $name {
67 -re "type = int A::\\*\r\n$gdb_prompt $" {
72 # print pointer to data member
74 set name "print pmi (A::j) "
75 gdb_test_multiple "print pmi" $name {
76 -re "$vhn = &A::j\r\n$gdb_prompt $" {
79 -re "$vhn = \\(int ?\\( ?A::\\*\\)\\) &A::j\r\n$gdb_prompt $" {
82 -re "$vhn = \\(int ?\\( ?A::\\*\\)\\) ?&A::j ?\\+ ?1 bytes\r\n$gdb_prompt $" {
83 # gcc 2.95.3 -gdwarf-2
84 kfail "gdb/NNNN" $name
86 -re "$vhn = &A::j ?\\+ ?1 bytes\r\n$gdb_prompt $" {
88 kfail "gdb/NNNN" $name
90 -re "$vhn = not implemented: member type in c_val_print\r\n$gdb_prompt $" {
91 # gcc HEAD 2004-01-11 05:33:21 -gdwarf-2
92 # gcc HEAD 2004-01-11 05:33:21 -gstabs+
93 kfail "gdb/NNNN" $name
95 -re "$vhn = \\(int ?\\( A::\\*\\)\\) 536870920\r\n$gdb_prompt $" {
96 # the value is 0x20000008 hex. 0x20000000 is an internal flag.
97 # Use '|' to add in more values as needed.
99 kfail "gdb/NNNN" $name
103 # print dereferenced pointer to data member
105 set name "print a.*pmi (A::j)"
106 gdb_test_multiple "print a.*pmi" $name {
107 -re "$vhn = 121\r\n$gdb_prompt $" {
110 -re "$vhn = 855638016\r\n$gdb_prompt $" {
111 # gcc 2.95.3 -gdwarf-2
112 # gcc 2.95.3 -gstabs+
113 kfail "gdb/NNNN" $name
115 -re "not implemented: member types in unpack_long\r\n$gdb_prompt $" {
116 # gcc HEAD 2004-01-10 -gdwarf-2
117 # gcc HEAD 2004-01-10 -gstabs+
118 kfail "gdb/NNNN" $name
122 # print dereferenced pointer to data member
123 # this time, dereferenced through a pointer
125 set name "print a_p->*pmi (A::j)"
126 gdb_test_multiple "print a_p->*pmi" $name {
127 -re "$vhn = 121\r\n$gdb_prompt $" {
130 -re "$vhn = 855638016\r\n$gdb_prompt $" {
131 # gcc 2.95.3 -gdwarf-2
132 # gcc 2.95.3 -gstabs+
133 kfail "gdb/NNNN" $name
135 -re "not implemented: member types in unpack_long\r\n$gdb_prompt $" {
136 # gcc HEAD 2004-01-10 -gdwarf-2
137 # gcc HEAD 2004-01-10 -gstabs+
138 kfail "gdb/NNNN" $name
142 # set the pointer to a different data member
144 set name "set var pmi = &A::jj"
145 gdb_test_multiple "set var pmi = &A::jj" $name {
146 -re "Invalid cast.\r\n$gdb_prompt $" {
147 # gcc HEAD 2004-01-10 -gdwarf-2
148 # gcc HEAD 2004-01-10 -gstabs+
149 kfail "gdb/NNNN" $name
151 -re "set var pmi = &A::jj\r\n$gdb_prompt $" {
152 # I have to match the echo'ed input explicitly here.
153 # If I leave it out, the pattern becomes too general
154 # and matches anything that ends in "$gdb_prompt $".
159 # print the pointer again
161 set name "print pmi (A::jj)"
162 gdb_test_multiple "print pmi" $name {
163 -re "$vhn = &A::jj\r\n$gdb_prompt $" {
166 -re "$vhn = \\(int ?\\( ?A::\\*\\)\\) &A::jj\r\n$gdb_prompt $" {
169 -re "$vhn = not implemented: member type in c_val_print\r\n$gdb_prompt $" {
170 # gcc HEAD 2004-01-11 05:33:21 -gdwarf-2
171 # gcc HEAD 2004-01-11 05:33:21 -gstabs+
172 kfail "gdb/NNNN" $name
174 -re "$vhn = \\(int ?\\( A::\\*\\)\\) 536870924\r\n$gdb_prompt $" {
175 # the value is 0x20000008 hex. 0x20000000 is an internal flag.
176 # Use '|' to add in more values as needed.
178 kfail "gdb/NNNN" $name
182 # print dereferenced pointer to data member again
184 set name "print a.*pmi (A::jj)"
185 gdb_test_multiple "print a.*pmi" $name {
186 -re "$vhn = 1331\r\n$gdb_prompt $" {
189 -re "not implemented: member types in unpack_long\r\n$gdb_prompt $" {
190 # gcc HEAD 2004-01-10 -gdwarf-2
191 # gcc HEAD 2004-01-10 -gstabs+
192 kfail "gdb/NNNN" $name
196 # set the pointer to data member back to A::j
198 set name "set var pmi = &A::j"
199 gdb_test_multiple "set var pmi = &A::j" $name {
200 -re "Invalid cast.\r\n$gdb_prompt $" {
201 # gcc HEAD 2004-01-10 -gdwarf-2
202 # gcc HEAD 2004-01-10 -gstabs+
203 kfail "gdb/NNNN" $name
205 -re "set var pmi = &A::j\r\n$gdb_prompt $" {
206 # I have to match the echo'ed input explicitly here.
207 # If I leave it out, the pattern becomes too general
208 # and matches anything that ends in "$gdb_prompt $".
213 # print dereferenced pointer to data member yet again (extra check, why not)
215 set name "print a.*pmi (A::j) (again)"
216 gdb_test_multiple "print a.*pmi" $name {
217 -re "$vhn = 121\r\n$gdb_prompt $" {
220 -re "not implemented: member types in unpack_long\r\n$gdb_prompt $" {
221 # gcc HEAD 2004-01-10 -gdwarf-2
222 # gcc HEAD 2004-01-10 -gstabs+
223 kfail "gdb/NNNN" $name
227 # Set the data member pointed to.
229 set name "print a.*pmi = 33"
230 gdb_test_multiple "print a.*pmi = 33" $name {
231 -re "$vhn = 33\r\n$gdb_prompt $" {
234 -re "not implemented: member types in unpack_long\r\n$gdb_prompt $" {
235 # gcc HEAD 2004-01-10 -gdwarf-2
236 # gcc HEAD 2004-01-10 -gstabs+
237 kfail "gdb/NNNN" $name
241 # Now check that the data really was changed
243 set name "print a.*pmi (A::j) (33)"
244 gdb_test_multiple "print a.*pmi" $name {
245 -re "$vhn = 33\r\n$gdb_prompt $" {
248 -re "not implemented: member types in unpack_long\r\n$gdb_prompt $" {
249 # gcc HEAD 2004-01-10 -gdwarf-2
250 # gcc HEAD 2004-01-10 -gstabs+
251 kfail "gdb/NNNN" $name
255 # Double-check by printing a.
257 set name "print a (j = 33)"
258 gdb_test_multiple "print a" $name {
259 -re "$vhn = \{c = 120 'x', j = 33, jj = 1331, (static|static int) s = 10, (_vptr.A|_vptr\\$) = ($hex|$hex <A virtual table>)\}\r\n$gdb_prompt $" {
262 -re "$vhn = \{c = 120 'x', j = 33, jj = 1331, (static|static int) s = 10, Virtual table at $hex\}\r\n$gdb_prompt $" {
265 -re "$vhn = \{(_vptr.A|_vptr\\$) = $hex, c = 120 'x', j = 33, jj = 1331, (static|static int) s = 10\}\r\n$gdb_prompt $" {
268 -re "$vhn = \{(_vptr.A|_vptr\\$) = $hex, c = 120 'x', j = 121, jj = 1331, (static|static int) s = 10\}\r\n$gdb_prompt $" {
269 # gcc HEAD 2004-01-10 -gdwarf-2
270 # gcc HEAD 2004-01-10 -gstabs+
271 kfail "gdb/NNNN" $name
275 # Set the data member pointed to, using ->*
277 set name "print a_p->*pmi = 44"
278 gdb_test_multiple "print a_p->*pmi = 44" $name {
279 -re "$vhn = 44\r\n$gdb_prompt $" {
282 -re "not implemented: member types in unpack_long\r\n$gdb_prompt $" {
283 # gcc HEAD 2004-01-10 -gdwarf-2
284 # gcc HEAD 2004-01-10 -gstabs+
285 kfail "gdb/NNNN" $name
289 # Check that the data really was changed
291 set name "print a_p->*pmi (44)"
292 gdb_test_multiple "print a_p->*pmi" $name {
293 -re "$vhn = 44\r\n$gdb_prompt $" {
296 -re "not implemented: member types in unpack_long\r\n$gdb_prompt $" {
297 # gcc HEAD 2004-01-10 -gdwarf-2
298 # gcc HEAD 2004-01-10 -gstabs+
299 kfail "gdb/NNNN" $name
303 # Double-check by printing a.
305 set name "print a (j = 44)"
306 gdb_test_multiple "print a" $name {
307 -re "$vhn = \{c = 120 'x', j = 44, jj = 1331, (static|static int) s = 10, (_vptr.A|_vptr\\$) = ($hex|$hex <A virtual table>)\}\r\n$gdb_prompt $" {
310 -re "$vhn = \{c = 120 'x', j = 44, jj = 1331, (static|static int) s = 10, Virtual table at $hex\}\r\n$gdb_prompt $" {
313 -re "$vhn = \{(_vptr.A|_vptr\\$) = $hex, c = 120 'x', j = 44, jj = 1331, (static|static int) s = 10\}\r\n$gdb_prompt $" {
316 -re "$vhn = \{(_vptr.A|_vptr\\$) = $hex, c = 120 'x', j = 121, jj = 1331, (static|static int) s = 10\}\r\n$gdb_prompt $" {
317 # gcc HEAD 2004-01-10 -gdwarf-2
318 # gcc HEAD 2004-01-10 -gstabs+
319 kfail "gdb/NNNN" $name
323 # ptype the dereferenced pointer to member.
325 set name "ptype a.*pmi"
326 gdb_test_multiple "ptype a.*pmi" $name {
327 -re "type = int\r\n$gdb_prompt" {
330 -re "not implemented: member types in unpack_long\r\n$gdb_prompt $" {
331 # gcc HEAD 2004-01-10 -gdwarf-2
332 # gcc HEAD 2004-01-10 -gstabs+
333 kfail "gdb/NNNN" $name
337 # dereference the pointer to data member without any object
338 # this is not allowed: a pmi must be bound to an object to dereference
340 set name "print *pmi"
341 gdb_test_multiple "print *pmi" $name {
342 -re "Attempt to dereference pointer to member without an object\r\n$gdb_prompt $" {
345 -re "Cannot access memory at address 0x4\r\n$gdb_prompt $" {
346 # gcc 2.95.3 -gstabs+
347 kfail "gdb/NNNN" $name
349 -re "Cannot access memory at address 0x8\r\n$gdb_prompt $" {
350 # gcc 3.3.2 -gdwarf-2
352 kfail "gdb/NNNN" $name
356 # dereference the pointer to data member without any object
357 # this is not allowed: a pmi must be bound to an object to dereference
359 set name "ptype *pmi"
360 gdb_test_multiple "ptype *pmi" $name {
361 -re "Attempt to dereference pointer to member without an object\r\n$gdb_prompt $" {
364 -re "type = int A::\r\n$gdb_prompt $" {
365 # gcc 2.95.3 -gstabs+
366 # gcc HEAD 2004-01-10 -gdwarf-2
367 # gcc HEAD 2004-01-10 -gstabs+
368 kfail "gdb/NNNN" $name
372 # Check cast of pointer to member to integer.
373 # This is similar to "offset-of".
374 # such as "A a; print (size_t) &A.j - (size_t) &A".
376 set name "print (int) pmi"
377 gdb_test_multiple "print (int) pmi" $name {
378 -re "$vhn = (4|8|12)\r\n$gdb_prompt" {
383 # Check "(int) pmi" explicitly for equality.
385 set name "print ((int) pmi) == ((char *) &a.j - (char *) &a)"
386 gdb_test_multiple "print ((int) pmi) == ((char *) &a.j - (char *) & a)" $name {
387 -re "$vhn = true\r\n$gdb_prompt" {
392 # ==========================
393 # pointer to member function
394 # ==========================
396 # ptype a pointer to a method
399 gdb_test_multiple "ptype pmf" $name {
400 -re "type = int \\( ?A::\\*\\)\\(A \\*, int\\)\r\n$gdb_prompt $" {
403 -re "type = int \\( ?A::\\*\\)\\(void\\)\r\n$gdb_prompt $" {
405 kfail "gdb/NNNN" $name
407 -re "type = struct \{.*\}\r\n$gdb_prompt $" {
408 # gcc 2.95.3 -gdwarf-2
409 # gcc 2.95.3 -gstabs+
410 # gcc 3.2.2 -gdwarf-2
412 # gcc HEAD 2004-01-10 -gdwarf-2
413 # gcc HEAD 2004-01-10 -gstabs+
414 kfail "gdb/NNNN" $name
418 # print a pointer to a method
421 gdb_test_multiple "print pmf" $name {
422 -re "$vhn = $hex <A::bar\\(int\\)>\r\n$gdb_prompt $" {
425 -re "$vhn = .*not supported with HP aCC.*\r\n$gdb_prompt $" {
427 kfail "gdb/NNNN" $name
429 -re "$vhn = \{.*\}\r\n$gdb_prompt $" {
430 # gcc 2.95.3 -gdwarf-2
431 # gcc 2.95.3 -gstabs+
432 # gcc 3.2.2 -gdwarf-2
434 # gcc HEAD 2004-01-10 -gdwarf-2
435 # gcc HEAD 2004-01-10 -gstabs+
436 kfail "gdb/NNNN" $name
440 # ptype a pointer to a pointer to a method
442 set name "ptype pmf_p"
443 gdb_test_multiple "ptype pmf_p" $name {
444 -re "type = int \\( ?A::\\*\\*\\)\\(A \\*, int\\)\r\n$gdb_prompt $" {
447 -re "type = int \\( ?A::\\*\\*\\)\\(void\\)\r\n$gdb_prompt $" {
449 kfail "gdb/NNNN" $name
451 -re "type = struct \{.*\} \\*\r\n$gdb_prompt $" {
452 # gcc 2.95.3 -gdwarf-2
453 # gcc 2.95.3 -gstabs+
454 # gcc 3.2.2 -gdwarf-2
456 # gcc HEAD 2004-01-10 -gdwarf-2
457 # gcc HEAD 2004-01-10 -gstabs+
458 kfail "gdb/NNNN" $name
462 # print a pointer to a pointer to a method
464 set name "print pmf_p"
465 gdb_test_multiple "print pmf_p" $name {
466 -re "$vhn = \\(int \\( ?A::\\*\\*\\)\\)\\(int\\)\\) $hex\r\n$gdb_prompt $" {
469 -re "$vhn = \\(PMF \\*\\) $hex\r\n$gdb_prompt $" {
472 -re "$vhn = \\(int \\( ?A::\\*\\*\\)\\(void\\)\\) $hex\r\n$gdb_prompt $" {
474 kfail "gdb/NNNN" $name
476 -re "$vhn = \\(struct \{.*\} \\*\\) $hex\r\n$gdb_prompt $" {
477 # gcc 2.95.3 -gdwarf-2
478 kfail "gdb/NNNN" $name
482 # print dereferenced pointer to method
484 set name "print a.*pmf"
485 gdb_test_multiple "print a.*pmf" $name {
486 -re "$vhn = {int \\(A \\*, int\\)} $hex <A::bar\\(int\\)>\r\n$gdb_prompt $" {
489 -re "Pointers to methods not supported with HP aCC\r\n$gdb_prompt $" {
491 kfail "gdb/NNNN" $name
493 -re "Value can't be converted to integer.\r\n$gdb_prompt $" {
494 # gcc 2.95.3 -gdwarf-2
495 # gcc 2.95.3 -gstabs+
496 # gcc 3.2.2 -gdwarf-2
498 # gcc HEAD 2004-01-10 -gdwarf-2
499 # gcc HEAD 2004-01-10 -gstabs+
500 kfail "gdb/NNNN" $name
504 # print dereferenced pointer to method, using ->*
506 set name "print a_p->*pmf"
507 gdb_test_multiple "print a_p->*pmf" $name {
508 -re "$vhn = {int \\(A \\*, int\\)} $hex <A::bar\\(int\\)>\r\n$gdb_prompt $" {
511 -re "Pointers to methods not supported with HP aCC\r\n$gdb_prompt $" {
513 kfail "gdb/NNNN" $name
515 -re "Value can't be converted to integer.\r\n$gdb_prompt $" {
516 # gcc 2.95.3 -gdwarf-2
517 # gcc 2.95.3 -gstabs+
518 # gcc 3.2.2 -gdwarf-2
520 # gcc HEAD 2004-01-10 -gdwarf-2
521 # gcc HEAD 2004-01-10 -gstabs+
522 kfail "gdb/NNNN" $name
526 # set the pointer to data member
528 set name "set var pmf = &A::foo"
529 gdb_test_multiple "set var pmf = &A::foo" $name {
530 -re "set var pmf = &A::foo\r\n$gdb_prompt $" {
531 # I have to match the echo'ed input explicitly here.
532 # If I leave it out, the pattern becomes too general
533 # and matches anything that ends in "$gdb_prompt $".
536 -re "Invalid cast.\r\n$gdb_prompt $" {
537 # gcc 2.95.3 -gdwarf-2
538 # gcc 2.95.3 -gstabs+
539 # gcc 3.2.2 -gdwarf-2
541 # gcc HEAD 2004-01-10 -gdwarf-2
542 # gcc HEAD 2004-01-10 -gstabs+
543 kfail "gdb/NNNN" $name
545 -re "Assignment to pointers to methods not implemented with HP aCC\r\n$gdb_prompt $" {
546 kfail "gdb/NNNN" $name
550 # dereference the pointer to data member without any object
551 # this is not allowed: a pmf must be bound to an object to dereference
553 set name "print *pmf"
554 gdb_test_multiple "print *pmf" $name {
555 -re "Attempt to dereference pointer to member without an object\r\n$gdb_prompt $" {
558 -re "Structure has no component named operator\\*.\r\n$gdb_prompt $" {
559 # gcc 2.95.3 -gdwarf-2
560 # gcc 2.95.3 -gstabs+
561 # gcc 3.3.2 -gdwarf-2
563 # gcc HEAD 2004-01-10 -gdwarf-2
564 # gcc HEAD 2004-01-10 -gstabs+
565 kfail "gdb/NNNN" $name
569 # dereference the pointer to data member without any object
570 # this is not allowed: a pmf must be bound to an object to dereference
572 set name "ptype *pmf"
573 gdb_test_multiple "ptype *pmf" $name {
574 -re "Attempt to dereference pointer to member without an object\r\n$gdb_prompt $" {
577 -re "Structure has no component named operator\\*.\r\n$gdb_prompt $" {
578 # gcc 2.95.3 -gdwarf-2
579 # gcc 2.95.3 -gstabs+
580 # gcc 3.3.2 -gdwarf-2
582 # gcc HEAD 2004-01-10 -gdwarf-2
583 # gcc HEAD 2004-01-10 -gstabs+
584 kfail "gdb/NNNN" $name
588 # Call a function through a pmf.
590 set name "print (a.*pmf)(3)"
591 gdb_test_multiple "print (a.*pmf)(3)" $name {
592 -re "$vhn = 50\r\n$gdb_prompt $" {
595 -re "Not implemented: function invocation through pointer to method with HP aCC\r\n$gdb_prompt $" {
597 kfail "gdb/NNNN" $name
599 -re "Value can't be converted to integer.\r\n$gdb_prompt $" {
600 # gcc 2.95.3 -gdwarf-2
601 # gcc 2.95.3 -gstabs+
602 # gcc 3.3.2 -gdwarf-2
604 # gcc HEAD 2004-01-10 -gdwarf-2
605 # gcc HEAD 2004-01-10 -gstabs+
606 kfail "gdb/NNNN" $name
610 # Print out a pointer to data member which requires looking into
612 gdb_test "print diamond_pmi" "$vhn = &Base::x"
613 gdb_test "print diamond.*diamond_pmi" "$vhn = 77"
615 # Examine some more complicated pmfs, which require adjusting "this"
616 # and looking through virtual tables.
618 # These two have a different object adjustment, but call the same method.
619 gdb_test "print diamond.*left_pmf" \
620 "$vhn = {int \\(Diamond \\*\\)} $hex <Base::get_x\\((void|)\\)>"
621 gdb_test "print diamond.*right_pmf" \
622 "$vhn = {int \\(Diamond \\*\\)} $hex <Base::get_x\\((void|)\\)>"
624 gdb_test "print (diamond.*left_pmf) ()" "$vhn = 77"
625 gdb_test "print (diamond.*right_pmf) ()" "$vhn = 88"
627 # These two point to different methods, although they have the same
628 # virtual table offsets.
629 gdb_test "print diamond.*left_vpmf" \
630 "$vhn = {int \\(Diamond \\*\\)} $hex <Left::vget\\((void|)\\)>"
631 gdb_test "print diamond.*right_vpmf" \
632 "$vhn = {int \\(Diamond \\*\\)} $hex <Right::vget\\((void|)\\)>"
634 gdb_test "print (diamond.*left_vpmf) ()" "$vhn = 177"
635 gdb_test "print (diamond.*left_base_vpmf) ()" "$vhn = 2077"
636 gdb_test "print (diamond.*right_vpmf) ()" "$vhn = 288"
638 # We should be able to figure out left_vpmf even without an object,
639 # because it comes from a non-virtual base. The same for right_vpmf.
640 gdb_test "print left_vpmf" "$vhn = &virtual Left::vget\\(\\)"
641 gdb_test "print right_vpmf" "$vhn = &virtual Right::vget\\(\\)"
643 # But we should gracefully fail to figure out base_vpmf, because
644 # its runtime type is more derived than its static type. This
645 # is a valid but unspecified cast (it is value preserving, i.e.
646 # can be casted back to the correct type and used).
647 gdb_test "print base_vpmf" \
648 "$vhn = &virtual table offset \[0-9\]*, this adjustment -\[0-9\]*"
650 # Make sure we parse this correctly; it's invalid.
651 gdb_test "print diamond.*left_vpmf ()" \
652 "Invalid data type for function to be called\\."
654 # NULL pointer to member tests.
655 gdb_test "print null_pmi" "$vhn = NULL"
656 gdb_test "print null_pmi = &A::j" "$vhn = &A::j"
657 gdb_test "print null_pmi = 0" "$vhn = NULL"
659 gdb_test "print null_pmf" "$vhn = NULL"
660 gdb_test "print null_pmf = &A::foo" "$vhn = $hex <A::foo ?\\(int\\)>"
661 gdb_test "print null_pmf = 0" "$vhn = NULL"