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 2 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, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 # Tests for pointer-to-member support
20 # Written by Satish Pai <pai@apollo.hp.com> 1997-08-19
21 # Rewritten by Michael Chastain <mec.gnu@mindspring.com> 2004-01-11
29 if { [skip_cplus_tests] } { continue }
34 set testfile "member-ptr"
35 set srcfile ${testfile}.cc
36 set binfile ${objdir}/${subdir}/${testfile}
38 if [get_compiler_info ${binfile} "c++"] {
42 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug c++}] != "" } {
43 untested member-ptr.exp
49 gdb_reinitialize_dir $srcdir/$subdir
52 if ![runto_main] then {
53 perror "couldn't run to breakpoint"
57 gdb_breakpoint [gdb_get_line_number "Breakpoint 1 here"]
58 gdb_continue_to_breakpoint "continue to pmi = NULL"
60 # ======================
61 # pointer to member data
62 # ======================
64 # ptype on pointer to data member
66 set name "ptype pmi (A::j)"
67 gdb_test_multiple "ptype pmi" $name {
68 -re "type = int A::\\*\r\n$gdb_prompt $" {
73 # print pointer to data member
75 set name "print pmi (A::j) "
76 gdb_test_multiple "print pmi" $name {
77 -re "$vhn = &A::j\r\n$gdb_prompt $" {
80 -re "$vhn = \\(int ?\\( ?A::\\*\\)\\) &A::j\r\n$gdb_prompt $" {
83 -re "$vhn = \\(int ?\\( ?A::\\*\\)\\) ?&A::j ?\\+ ?1 bytes\r\n$gdb_prompt $" {
84 # gcc 2.95.3 -gdwarf-2
85 kfail "gdb/NNNN" $name
87 -re "$vhn = &A::j ?\\+ ?1 bytes\r\n$gdb_prompt $" {
89 kfail "gdb/NNNN" $name
91 -re "$vhn = not implemented: member type in c_val_print\r\n$gdb_prompt $" {
92 # gcc HEAD 2004-01-11 05:33:21 -gdwarf-2
93 # gcc HEAD 2004-01-11 05:33:21 -gstabs+
94 kfail "gdb/NNNN" $name
96 -re "$vhn = \\(int ?\\( A::\\*\\)\\) 536870920\r\n$gdb_prompt $" {
97 # the value is 0x20000008 hex. 0x20000000 is an internal flag.
98 # Use '|' to add in more values as needed.
100 kfail "gdb/NNNN" $name
104 # print dereferenced pointer to data member
106 set name "print a.*pmi (A::j)"
107 gdb_test_multiple "print a.*pmi" $name {
108 -re "$vhn = 121\r\n$gdb_prompt $" {
111 -re "$vhn = 855638016\r\n$gdb_prompt $" {
112 # gcc 2.95.3 -gdwarf-2
113 # gcc 2.95.3 -gstabs+
114 kfail "gdb/NNNN" $name
116 -re "not implemented: member types in unpack_long\r\n$gdb_prompt $" {
117 # gcc HEAD 2004-01-10 -gdwarf-2
118 # gcc HEAD 2004-01-10 -gstabs+
119 kfail "gdb/NNNN" $name
123 # print dereferenced pointer to data member
124 # this time, dereferenced through a pointer
126 set name "print a_p->*pmi (A::j)"
127 gdb_test_multiple "print a_p->*pmi" $name {
128 -re "$vhn = 121\r\n$gdb_prompt $" {
131 -re "$vhn = 855638016\r\n$gdb_prompt $" {
132 # gcc 2.95.3 -gdwarf-2
133 # gcc 2.95.3 -gstabs+
134 kfail "gdb/NNNN" $name
136 -re "not implemented: member types in unpack_long\r\n$gdb_prompt $" {
137 # gcc HEAD 2004-01-10 -gdwarf-2
138 # gcc HEAD 2004-01-10 -gstabs+
139 kfail "gdb/NNNN" $name
143 # set the pointer to a different data member
145 set name "set var pmi = &A::jj"
146 gdb_test_multiple "set var pmi = &A::jj" $name {
147 -re "Invalid cast.\r\n$gdb_prompt $" {
148 # gcc HEAD 2004-01-10 -gdwarf-2
149 # gcc HEAD 2004-01-10 -gstabs+
150 kfail "gdb/NNNN" $name
152 -re "set var pmi = &A::jj\r\n$gdb_prompt $" {
153 # I have to match the echo'ed input explicitly here.
154 # If I leave it out, the pattern becomes too general
155 # and matches anything that ends in "$gdb_prompt $".
160 # print the pointer again
162 set name "print pmi (A::jj)"
163 gdb_test_multiple "print pmi" $name {
164 -re "$vhn = &A::jj\r\n$gdb_prompt $" {
167 -re "$vhn = \\(int ?\\( ?A::\\*\\)\\) &A::jj\r\n$gdb_prompt $" {
170 -re "$vhn = not implemented: member type in c_val_print\r\n$gdb_prompt $" {
171 # gcc HEAD 2004-01-11 05:33:21 -gdwarf-2
172 # gcc HEAD 2004-01-11 05:33:21 -gstabs+
173 kfail "gdb/NNNN" $name
175 -re "$vhn = \\(int ?\\( A::\\*\\)\\) 536870924\r\n$gdb_prompt $" {
176 # the value is 0x20000008 hex. 0x20000000 is an internal flag.
177 # Use '|' to add in more values as needed.
179 kfail "gdb/NNNN" $name
183 # print dereferenced pointer to data member again
185 set name "print a.*pmi (A::jj)"
186 gdb_test_multiple "print a.*pmi" $name {
187 -re "$vhn = 1331\r\n$gdb_prompt $" {
190 -re "not implemented: member types in unpack_long\r\n$gdb_prompt $" {
191 # gcc HEAD 2004-01-10 -gdwarf-2
192 # gcc HEAD 2004-01-10 -gstabs+
193 kfail "gdb/NNNN" $name
197 # set the pointer to data member back to A::j
199 set name "set var pmi = &A::j"
200 gdb_test_multiple "set var pmi = &A::j" $name {
201 -re "Invalid cast.\r\n$gdb_prompt $" {
202 # gcc HEAD 2004-01-10 -gdwarf-2
203 # gcc HEAD 2004-01-10 -gstabs+
204 kfail "gdb/NNNN" $name
206 -re "set var pmi = &A::j\r\n$gdb_prompt $" {
207 # I have to match the echo'ed input explicitly here.
208 # If I leave it out, the pattern becomes too general
209 # and matches anything that ends in "$gdb_prompt $".
214 # print dereferenced pointer to data member yet again (extra check, why not)
216 set name "print a.*pmi (A::j) (again)"
217 gdb_test_multiple "print a.*pmi" $name {
218 -re "$vhn = 121\r\n$gdb_prompt $" {
221 -re "not implemented: member types in unpack_long\r\n$gdb_prompt $" {
222 # gcc HEAD 2004-01-10 -gdwarf-2
223 # gcc HEAD 2004-01-10 -gstabs+
224 kfail "gdb/NNNN" $name
228 # Set the data member pointed to.
230 set name "print a.*pmi = 33"
231 gdb_test_multiple "print a.*pmi = 33" $name {
232 -re "$vhn = 33\r\n$gdb_prompt $" {
235 -re "not implemented: member types in unpack_long\r\n$gdb_prompt $" {
236 # gcc HEAD 2004-01-10 -gdwarf-2
237 # gcc HEAD 2004-01-10 -gstabs+
238 kfail "gdb/NNNN" $name
242 # Now check that the data really was changed
244 set name "print a.*pmi (A::j) (33)"
245 gdb_test_multiple "print a.*pmi" $name {
246 -re "$vhn = 33\r\n$gdb_prompt $" {
249 -re "not implemented: member types in unpack_long\r\n$gdb_prompt $" {
250 # gcc HEAD 2004-01-10 -gdwarf-2
251 # gcc HEAD 2004-01-10 -gstabs+
252 kfail "gdb/NNNN" $name
256 # Double-check by printing a.
258 set name "print a (j = 33)"
259 gdb_test_multiple "print a" $name {
260 -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 $" {
263 -re "$vhn = \{c = 120 'x', j = 33, jj = 1331, (static|static int) s = 10, Virtual table at $hex\}\r\n$gdb_prompt $" {
266 -re "$vhn = \{(_vptr.A|_vptr\\$) = $hex, c = 120 'x', j = 33, jj = 1331, (static|static int) s = 10\}\r\n$gdb_prompt $" {
269 -re "$vhn = \{(_vptr.A|_vptr\\$) = $hex, c = 120 'x', j = 121, jj = 1331, (static|static int) s = 10\}\r\n$gdb_prompt $" {
270 # gcc HEAD 2004-01-10 -gdwarf-2
271 # gcc HEAD 2004-01-10 -gstabs+
272 kfail "gdb/NNNN" $name
276 # Set the data member pointed to, using ->*
278 set name "print a_p->*pmi = 44"
279 gdb_test_multiple "print a_p->*pmi = 44" $name {
280 -re "$vhn = 44\r\n$gdb_prompt $" {
283 -re "not implemented: member types in unpack_long\r\n$gdb_prompt $" {
284 # gcc HEAD 2004-01-10 -gdwarf-2
285 # gcc HEAD 2004-01-10 -gstabs+
286 kfail "gdb/NNNN" $name
290 # Check that the data really was changed
292 set name "print a_p->*pmi (44)"
293 gdb_test_multiple "print a_p->*pmi" $name {
294 -re "$vhn = 44\r\n$gdb_prompt $" {
297 -re "not implemented: member types in unpack_long\r\n$gdb_prompt $" {
298 # gcc HEAD 2004-01-10 -gdwarf-2
299 # gcc HEAD 2004-01-10 -gstabs+
300 kfail "gdb/NNNN" $name
304 # Double-check by printing a.
306 set name "print a (j = 44)"
307 gdb_test_multiple "print a" $name {
308 -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 $" {
311 -re "$vhn = \{c = 120 'x', j = 44, jj = 1331, (static|static int) s = 10, Virtual table at $hex\}\r\n$gdb_prompt $" {
314 -re "$vhn = \{(_vptr.A|_vptr\\$) = $hex, c = 120 'x', j = 44, jj = 1331, (static|static int) s = 10\}\r\n$gdb_prompt $" {
317 -re "$vhn = \{(_vptr.A|_vptr\\$) = $hex, c = 120 'x', j = 121, jj = 1331, (static|static int) s = 10\}\r\n$gdb_prompt $" {
318 # gcc HEAD 2004-01-10 -gdwarf-2
319 # gcc HEAD 2004-01-10 -gstabs+
320 kfail "gdb/NNNN" $name
324 # ptype the dereferenced pointer to member.
326 set name "ptype a.*pmi"
327 gdb_test_multiple "ptype a.*pmi" $name {
328 -re "type = int\r\n$gdb_prompt" {
331 -re "not implemented: member types in unpack_long\r\n$gdb_prompt $" {
332 # gcc HEAD 2004-01-10 -gdwarf-2
333 # gcc HEAD 2004-01-10 -gstabs+
334 kfail "gdb/NNNN" $name
338 # dereference the pointer to data member without any object
339 # this is not allowed: a pmi must be bound to an object to dereference
341 set name "print *pmi"
342 gdb_test_multiple "print *pmi" $name {
343 -re "Attempt to dereference pointer to member without an object\r\n$gdb_prompt $" {
346 -re "Cannot access memory at address 0x4\r\n$gdb_prompt $" {
347 # gcc 2.95.3 -gstabs+
348 kfail "gdb/NNNN" $name
350 -re "Cannot access memory at address 0x8\r\n$gdb_prompt $" {
351 # gcc 3.3.2 -gdwarf-2
353 kfail "gdb/NNNN" $name
357 # dereference the pointer to data member without any object
358 # this is not allowed: a pmi must be bound to an object to dereference
360 set name "ptype *pmi"
361 gdb_test_multiple "ptype *pmi" $name {
362 -re "Attempt to dereference pointer to member without an object\r\n$gdb_prompt $" {
365 -re "type = int A::\r\n$gdb_prompt $" {
366 # gcc 2.95.3 -gstabs+
367 # gcc HEAD 2004-01-10 -gdwarf-2
368 # gcc HEAD 2004-01-10 -gstabs+
369 kfail "gdb/NNNN" $name
373 # Check cast of pointer to member to integer.
374 # This is similar to "offset-of".
375 # such as "A a; print (size_t) &A.j - (size_t) &A".
377 set name "print (int) pmi"
378 gdb_test_multiple "print (int) pmi" $name {
379 -re "$vhn = (4|8|12)\r\n$gdb_prompt" {
384 # Check "(int) pmi" explicitly for equality.
386 set name "print ((int) pmi) == ((char *) &a.j - (char *) &a)"
387 gdb_test_multiple "print ((int) pmi) == ((char *) &a.j - (char *) & a)" $name {
388 -re "$vhn = true\r\n$gdb_prompt" {
393 # ==========================
394 # pointer to member function
395 # ==========================
397 # ptype a pointer to a method
400 gdb_test_multiple "ptype pmf" $name {
401 -re "type = int \\( ?A::\\*\\)\\(A \\*, int\\)\r\n$gdb_prompt $" {
404 -re "type = int \\( ?A::\\*\\)\\(void\\)\r\n$gdb_prompt $" {
406 kfail "gdb/NNNN" $name
408 -re "type = struct \{.*\}\r\n$gdb_prompt $" {
409 # gcc 2.95.3 -gdwarf-2
410 # gcc 2.95.3 -gstabs+
411 # gcc 3.2.2 -gdwarf-2
413 # gcc HEAD 2004-01-10 -gdwarf-2
414 # gcc HEAD 2004-01-10 -gstabs+
415 kfail "gdb/NNNN" $name
419 # print a pointer to a method
422 gdb_test_multiple "print pmf" $name {
423 -re "$vhn = $hex <A::bar\\(int\\)>\r\n$gdb_prompt $" {
426 -re "$vhn = .*not supported with HP aCC.*\r\n$gdb_prompt $" {
428 kfail "gdb/NNNN" $name
430 -re "$vhn = \{.*\}\r\n$gdb_prompt $" {
431 # gcc 2.95.3 -gdwarf-2
432 # gcc 2.95.3 -gstabs+
433 # gcc 3.2.2 -gdwarf-2
435 # gcc HEAD 2004-01-10 -gdwarf-2
436 # gcc HEAD 2004-01-10 -gstabs+
437 kfail "gdb/NNNN" $name
441 # ptype a pointer to a pointer to a method
443 set name "ptype pmf_p"
444 gdb_test_multiple "ptype pmf_p" $name {
445 -re "type = int \\( ?A::\\*\\*\\)\\(A \\*, int\\)\r\n$gdb_prompt $" {
448 -re "type = int \\( ?A::\\*\\*\\)\\(void\\)\r\n$gdb_prompt $" {
450 kfail "gdb/NNNN" $name
452 -re "type = struct \{.*\} \\*\r\n$gdb_prompt $" {
453 # gcc 2.95.3 -gdwarf-2
454 # gcc 2.95.3 -gstabs+
455 # gcc 3.2.2 -gdwarf-2
457 # gcc HEAD 2004-01-10 -gdwarf-2
458 # gcc HEAD 2004-01-10 -gstabs+
459 kfail "gdb/NNNN" $name
463 # print a pointer to a pointer to a method
465 set name "print pmf_p"
466 gdb_test_multiple "print pmf_p" $name {
467 -re "$vhn = \\(int \\( ?A::\\*\\*\\)\\)\\(int\\)\\) $hex\r\n$gdb_prompt $" {
470 -re "$vhn = \\(PMF \\*\\) $hex\r\n$gdb_prompt $" {
473 -re "$vhn = \\(int \\( ?A::\\*\\*\\)\\(void\\)\\) $hex\r\n$gdb_prompt $" {
475 kfail "gdb/NNNN" $name
477 -re "$vhn = \\(struct \{.*\} \\*\\) $hex\r\n$gdb_prompt $" {
478 # gcc 2.95.3 -gdwarf-2
479 kfail "gdb/NNNN" $name
483 # print dereferenced pointer to method
485 set name "print a.*pmf"
486 gdb_test_multiple "print a.*pmf" $name {
487 -re "$vhn = {int \\(A \\*, int\\)} $hex <A::bar\\(int\\)>\r\n$gdb_prompt $" {
490 -re "Pointers to methods not supported with HP aCC\r\n$gdb_prompt $" {
492 kfail "gdb/NNNN" $name
494 -re "Value can't be converted to integer.\r\n$gdb_prompt $" {
495 # gcc 2.95.3 -gdwarf-2
496 # gcc 2.95.3 -gstabs+
497 # gcc 3.2.2 -gdwarf-2
499 # gcc HEAD 2004-01-10 -gdwarf-2
500 # gcc HEAD 2004-01-10 -gstabs+
501 kfail "gdb/NNNN" $name
505 # print dereferenced pointer to method, using ->*
507 set name "print a_p->*pmf"
508 gdb_test_multiple "print a_p->*pmf" $name {
509 -re "$vhn = {int \\(A \\*, int\\)} $hex <A::bar\\(int\\)>\r\n$gdb_prompt $" {
512 -re "Pointers to methods not supported with HP aCC\r\n$gdb_prompt $" {
514 kfail "gdb/NNNN" $name
516 -re "Value can't be converted to integer.\r\n$gdb_prompt $" {
517 # gcc 2.95.3 -gdwarf-2
518 # gcc 2.95.3 -gstabs+
519 # gcc 3.2.2 -gdwarf-2
521 # gcc HEAD 2004-01-10 -gdwarf-2
522 # gcc HEAD 2004-01-10 -gstabs+
523 kfail "gdb/NNNN" $name
527 # set the pointer to data member
529 set name "set var pmf = &A::foo"
530 gdb_test_multiple "set var pmf = &A::foo" $name {
531 -re "set var pmf = &A::foo\r\n$gdb_prompt $" {
532 # I have to match the echo'ed input explicitly here.
533 # If I leave it out, the pattern becomes too general
534 # and matches anything that ends in "$gdb_prompt $".
537 -re "Invalid cast.\r\n$gdb_prompt $" {
538 # gcc 2.95.3 -gdwarf-2
539 # gcc 2.95.3 -gstabs+
540 # gcc 3.2.2 -gdwarf-2
542 # gcc HEAD 2004-01-10 -gdwarf-2
543 # gcc HEAD 2004-01-10 -gstabs+
544 kfail "gdb/NNNN" $name
546 -re "Assignment to pointers to methods not implemented with HP aCC\r\n$gdb_prompt $" {
547 kfail "gdb/NNNN" $name
551 # dereference the pointer to data member without any object
552 # this is not allowed: a pmf must be bound to an object to dereference
554 set name "print *pmf"
555 gdb_test_multiple "print *pmf" $name {
556 -re "Attempt to dereference pointer to member without an object\r\n$gdb_prompt $" {
559 -re "Structure has no component named operator\\*.\r\n$gdb_prompt $" {
560 # gcc 2.95.3 -gdwarf-2
561 # gcc 2.95.3 -gstabs+
562 # gcc 3.3.2 -gdwarf-2
564 # gcc HEAD 2004-01-10 -gdwarf-2
565 # gcc HEAD 2004-01-10 -gstabs+
566 kfail "gdb/NNNN" $name
570 # dereference the pointer to data member without any object
571 # this is not allowed: a pmf must be bound to an object to dereference
573 set name "ptype *pmf"
574 gdb_test_multiple "ptype *pmf" $name {
575 -re "Attempt to dereference pointer to member without an object\r\n$gdb_prompt $" {
578 -re "Structure has no component named operator\\*.\r\n$gdb_prompt $" {
579 # gcc 2.95.3 -gdwarf-2
580 # gcc 2.95.3 -gstabs+
581 # gcc 3.3.2 -gdwarf-2
583 # gcc HEAD 2004-01-10 -gdwarf-2
584 # gcc HEAD 2004-01-10 -gstabs+
585 kfail "gdb/NNNN" $name
589 # Call a function through a pmf.
591 set name "print (a.*pmf)(3)"
592 gdb_test_multiple "print (a.*pmf)(3)" $name {
593 -re "$vhn = 50\r\n$gdb_prompt $" {
596 -re "Not implemented: function invocation through pointer to method with HP aCC\r\n$gdb_prompt $" {
598 kfail "gdb/NNNN" $name
600 -re "Value can't be converted to integer.\r\n$gdb_prompt $" {
601 # gcc 2.95.3 -gdwarf-2
602 # gcc 2.95.3 -gstabs+
603 # gcc 3.3.2 -gdwarf-2
605 # gcc HEAD 2004-01-10 -gdwarf-2
606 # gcc HEAD 2004-01-10 -gstabs+
607 kfail "gdb/NNNN" $name
611 # Print out a pointer to data member which requires looking into
613 gdb_test "print diamond_pmi" "$vhn = &Base::x"
614 gdb_test "print diamond.*diamond_pmi" "$vhn = 77"
616 # Examine some more complicated pmfs, which require adjusting "this"
617 # and looking through virtual tables.
619 # These two have a different object adjustment, but call the same method.
620 gdb_test "print diamond.*left_pmf" \
621 "$vhn = {int \\(Diamond \\*\\)} $hex <Base::get_x\\((void|)\\)>"
622 gdb_test "print diamond.*right_pmf" \
623 "$vhn = {int \\(Diamond \\*\\)} $hex <Base::get_x\\((void|)\\)>"
625 gdb_test "print (diamond.*left_pmf) ()" "$vhn = 77"
626 gdb_test "print (diamond.*right_pmf) ()" "$vhn = 88"
628 # These two point to different methods, although they have the same
629 # virtual table offsets.
630 gdb_test "print diamond.*left_vpmf" \
631 "$vhn = {int \\(Diamond \\*\\)} $hex <Left::vget\\((void|)\\)>"
632 gdb_test "print diamond.*right_vpmf" \
633 "$vhn = {int \\(Diamond \\*\\)} $hex <Right::vget\\((void|)\\)>"
635 gdb_test "print (diamond.*left_vpmf) ()" "$vhn = 177"
636 gdb_test "print (diamond.*left_base_vpmf) ()" "$vhn = 2077"
637 gdb_test "print (diamond.*right_vpmf) ()" "$vhn = 288"
639 # We should be able to figure out left_vpmf even without an object,
640 # because it comes from a non-virtual base. The same for right_vpmf.
641 gdb_test "print left_vpmf" "$vhn = &virtual Left::vget\\(\\)"
642 gdb_test "print right_vpmf" "$vhn = &virtual Right::vget\\(\\)"
644 # But we should gracefully fail to figure out base_vpmf, because
645 # its runtime type is more derived than its static type. This
646 # is a valid but unspecified cast (it is value preserving, i.e.
647 # can be casted back to the correct type and used).
648 gdb_test "print base_vpmf" \
649 "$vhn = &virtual table offset \[0-9\]*, this adjustment -\[0-9\]*"
651 # Make sure we parse this correctly; it's invalid.
652 gdb_test "print diamond.*left_vpmf ()" \
653 "Invalid data type for function to be called\\."
655 # NULL pointer to member tests.
656 gdb_test "print null_pmi" "$vhn = NULL"
657 gdb_test "print null_pmi = &A::j" "$vhn = &A::j"
658 gdb_test "print null_pmi = 0" "$vhn = NULL"
660 gdb_test "print null_pmf" "$vhn = NULL"
661 gdb_test "print null_pmf = &A::foo" "$vhn = $hex <A::foo ?\\(int\\)>"
662 gdb_test "print null_pmf = 0" "$vhn = NULL"