1 # Copyright 1998-2019 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
24 if { [skip_cplus_tests] } { continue }
29 if [get_compiler_info "c++"] {
33 if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug c++}]} {
37 if ![runto_main] then {
38 perror "couldn't run to breakpoint"
42 gdb_breakpoint [gdb_get_line_number "Breakpoint 1 here"]
43 gdb_continue_to_breakpoint "continue to pmi = NULL"
45 # ======================
46 # pointer to member data
47 # ======================
49 # ptype on pointer to data member
51 set name "ptype pmi (A::j)"
52 gdb_test_multiple "ptype pmi" $name {
53 -re "type = int A::\\*\r\n$gdb_prompt $" {
58 # print pointer to data member
60 set name "print pmi (A::j) "
61 gdb_test_multiple "print pmi" $name {
62 -re "$vhn = &A::j\r\n$gdb_prompt $" {
65 -re "$vhn = \\(int ?\\( ?A::\\*\\)\\) &A::j\r\n$gdb_prompt $" {
68 -re "$vhn = \\(int ?\\( ?A::\\*\\)\\) ?&A::j ?\\+ ?1 bytes\r\n$gdb_prompt $" {
69 # gcc 2.95.3 -gdwarf-2
70 kfail "gdb/NNNN" $name
72 -re "$vhn = &A::j ?\\+ ?1 bytes\r\n$gdb_prompt $" {
74 kfail "gdb/NNNN" $name
76 -re "$vhn = not implemented: member type in c_val_print\r\n$gdb_prompt $" {
77 # gcc HEAD 2004-01-11 05:33:21 -gdwarf-2
78 # gcc HEAD 2004-01-11 05:33:21 -gstabs+
79 kfail "gdb/NNNN" $name
83 # print dereferenced pointer to data member
85 set name "print a.*pmi (A::j)"
86 gdb_test_multiple "print a.*pmi" $name {
87 -re "$vhn = 121\r\n$gdb_prompt $" {
90 -re "$vhn = 855638016\r\n$gdb_prompt $" {
91 # gcc 2.95.3 -gdwarf-2
93 kfail "gdb/NNNN" $name
95 -re "not implemented: member types in unpack_long\r\n$gdb_prompt $" {
96 # gcc HEAD 2004-01-10 -gdwarf-2
97 # gcc HEAD 2004-01-10 -gstabs+
98 kfail "gdb/NNNN" $name
102 # print dereferenced pointer to data member
103 # this time, dereferenced through a pointer
105 set name "print a_p->*pmi (A::j)"
106 gdb_test_multiple "print a_p->*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 # set the pointer to a different data member
124 set name "set var pmi = &A::jj"
125 gdb_test_multiple "set var pmi = &A::jj" $name {
126 -re "Invalid cast.\r\n$gdb_prompt $" {
127 # gcc HEAD 2004-01-10 -gdwarf-2
128 # gcc HEAD 2004-01-10 -gstabs+
129 kfail "gdb/NNNN" $name
131 -re "set var pmi = &A::jj\r\n$gdb_prompt $" {
132 # I have to match the echo'ed input explicitly here.
133 # If I leave it out, the pattern becomes too general
134 # and matches anything that ends in "$gdb_prompt $".
139 # print the pointer again
141 set name "print pmi (A::jj)"
142 gdb_test_multiple "print pmi" $name {
143 -re "$vhn = &A::jj\r\n$gdb_prompt $" {
146 -re "$vhn = \\(int ?\\( ?A::\\*\\)\\) &A::jj\r\n$gdb_prompt $" {
149 -re "$vhn = not implemented: member type in c_val_print\r\n$gdb_prompt $" {
150 # gcc HEAD 2004-01-11 05:33:21 -gdwarf-2
151 # gcc HEAD 2004-01-11 05:33:21 -gstabs+
152 kfail "gdb/NNNN" $name
156 # print dereferenced pointer to data member again
158 set name "print a.*pmi (A::jj)"
159 gdb_test_multiple "print a.*pmi" $name {
160 -re "$vhn = 1331\r\n$gdb_prompt $" {
163 -re "not implemented: member types in unpack_long\r\n$gdb_prompt $" {
164 # gcc HEAD 2004-01-10 -gdwarf-2
165 # gcc HEAD 2004-01-10 -gstabs+
166 kfail "gdb/NNNN" $name
170 # set the pointer to data member back to A::j
172 set name "set var pmi = &A::j"
173 gdb_test_multiple "set var pmi = &A::j" $name {
174 -re "Invalid cast.\r\n$gdb_prompt $" {
175 # gcc HEAD 2004-01-10 -gdwarf-2
176 # gcc HEAD 2004-01-10 -gstabs+
177 kfail "gdb/NNNN" $name
179 -re "set var pmi = &A::j\r\n$gdb_prompt $" {
180 # I have to match the echo'ed input explicitly here.
181 # If I leave it out, the pattern becomes too general
182 # and matches anything that ends in "$gdb_prompt $".
187 # print dereferenced pointer to data member yet again (extra check, why not)
189 set name "print a.*pmi (A::j) (again)"
190 gdb_test_multiple "print a.*pmi" $name {
191 -re "$vhn = 121\r\n$gdb_prompt $" {
194 -re "not implemented: member types in unpack_long\r\n$gdb_prompt $" {
195 # gcc HEAD 2004-01-10 -gdwarf-2
196 # gcc HEAD 2004-01-10 -gstabs+
197 kfail "gdb/NNNN" $name
201 # Set the data member pointed to.
203 set name "print a.*pmi = 33"
204 gdb_test_multiple "print a.*pmi = 33" $name {
205 -re "$vhn = 33\r\n$gdb_prompt $" {
208 -re "not implemented: member types in unpack_long\r\n$gdb_prompt $" {
209 # gcc HEAD 2004-01-10 -gdwarf-2
210 # gcc HEAD 2004-01-10 -gstabs+
211 kfail "gdb/NNNN" $name
215 # Now check that the data really was changed
217 set name "print a.*pmi (A::j) (33)"
218 gdb_test_multiple "print a.*pmi" $name {
219 -re "$vhn = 33\r\n$gdb_prompt $" {
222 -re "not implemented: member types in unpack_long\r\n$gdb_prompt $" {
223 # gcc HEAD 2004-01-10 -gdwarf-2
224 # gcc HEAD 2004-01-10 -gstabs+
225 kfail "gdb/NNNN" $name
229 # Double-check by printing a.
231 set name "print a (j = 33)"
232 gdb_test_multiple "print a" $name {
233 -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 $" {
236 -re "$vhn = \{c = 120 'x', j = 33, jj = 1331, (static|static int) s = 10, Virtual table at $hex\}\r\n$gdb_prompt $" {
239 -re "$vhn = \{(_vptr.A|_vptr\\$) = ${hex}( <vtable for A.*>)?, c = 120 'x', j = 33, jj = 1331, (static|static int) s = 10\}\r\n$gdb_prompt $" {
242 -re "$vhn = \{(_vptr.A|_vptr\\$) = $hex, c = 120 'x', j = 121, jj = 1331, (static|static int) s = 10\}\r\n$gdb_prompt $" {
243 # gcc HEAD 2004-01-10 -gdwarf-2
244 # gcc HEAD 2004-01-10 -gstabs+
245 kfail "gdb/NNNN" $name
249 # Set the data member pointed to, using ->*
251 set name "print a_p->*pmi = 44"
252 gdb_test_multiple "print a_p->*pmi = 44" $name {
253 -re "$vhn = 44\r\n$gdb_prompt $" {
256 -re "not implemented: member types in unpack_long\r\n$gdb_prompt $" {
257 # gcc HEAD 2004-01-10 -gdwarf-2
258 # gcc HEAD 2004-01-10 -gstabs+
259 kfail "gdb/NNNN" $name
263 # Check that the data really was changed
265 set name "print a_p->*pmi (44)"
266 gdb_test_multiple "print a_p->*pmi" $name {
267 -re "$vhn = 44\r\n$gdb_prompt $" {
270 -re "not implemented: member types in unpack_long\r\n$gdb_prompt $" {
271 # gcc HEAD 2004-01-10 -gdwarf-2
272 # gcc HEAD 2004-01-10 -gstabs+
273 kfail "gdb/NNNN" $name
277 # Double-check by printing a.
279 set name "print a (j = 44)"
280 gdb_test_multiple "print a" $name {
281 -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 $" {
284 -re "$vhn = \{c = 120 'x', j = 44, jj = 1331, (static|static int) s = 10, Virtual table at $hex\}\r\n$gdb_prompt $" {
287 -re "$vhn = \{(_vptr.A|_vptr\\$) = ${hex}( <vtable for A.*>), c = 120 'x', j = 44, jj = 1331, (static|static int) s = 10\}\r\n$gdb_prompt $" {
290 -re "$vhn = \{(_vptr.A|_vptr\\$) = $hex, c = 120 'x', j = 121, jj = 1331, (static|static int) s = 10\}\r\n$gdb_prompt $" {
291 # gcc HEAD 2004-01-10 -gdwarf-2
292 # gcc HEAD 2004-01-10 -gstabs+
293 kfail "gdb/NNNN" $name
297 # ptype the dereferenced pointer to member.
299 set name "ptype a.*pmi"
300 gdb_test_multiple "ptype a.*pmi" $name {
301 -re "type = int\r\n$gdb_prompt" {
304 -re "not implemented: member types in unpack_long\r\n$gdb_prompt $" {
305 # gcc HEAD 2004-01-10 -gdwarf-2
306 # gcc HEAD 2004-01-10 -gstabs+
307 kfail "gdb/NNNN" $name
311 # dereference the pointer to data member without any object
312 # this is not allowed: a pmi must be bound to an object to dereference
314 set name "print *pmi"
315 gdb_test_multiple "print *pmi" $name {
316 -re "Attempt to dereference pointer to member without an object\r\n$gdb_prompt $" {
319 -re "Cannot access memory at address 0x4\r\n$gdb_prompt $" {
320 # gcc 2.95.3 -gstabs+
321 kfail "gdb/NNNN" $name
323 -re "Cannot access memory at address 0x8\r\n$gdb_prompt $" {
324 # gcc 3.3.2 -gdwarf-2
326 kfail "gdb/NNNN" $name
330 # dereference the pointer to data member without any object
331 # this is not allowed: a pmi must be bound to an object to dereference
333 set name "ptype *pmi"
334 gdb_test_multiple "ptype *pmi" $name {
335 -re "Attempt to dereference pointer to member without an object\r\n$gdb_prompt $" {
338 -re "type = int A::\r\n$gdb_prompt $" {
339 # gcc 2.95.3 -gstabs+
340 # gcc HEAD 2004-01-10 -gdwarf-2
341 # gcc HEAD 2004-01-10 -gstabs+
342 kfail "gdb/NNNN" $name
346 # Check cast of pointer to member to integer.
347 # This is similar to "offset-of".
348 # such as "A a; print (size_t) &A.j - (size_t) &A".
350 set name "print (int) pmi"
351 gdb_test_multiple "print (int) pmi" $name {
352 -re "$vhn = (4|8|12)\r\n$gdb_prompt" {
357 # Check "(int) pmi" explicitly for equality.
359 set name "print ((int) pmi) == ((char *) &a.j - (char *) &a)"
360 gdb_test_multiple "print ((int) pmi) == ((char *) &a.j - (char *) & a)" $name {
361 -re "$vhn = true\r\n$gdb_prompt" {
366 # Check pointers to data members, which are themselves pointers to
367 # functions. These behave like data members, not like pointers to
370 gdb_test "ptype diamond_pfunc_ptr" \
371 "type = int \\(\\*Diamond::\\*\\)\\(int\\)"
373 gdb_test "ptype diamond.*diamond_pfunc_ptr" \
374 "type = int \\(\\*\\)\\(int\\)"
376 # This one is invalid; () binds more tightly than .*, so it tries to
377 # call the member pointer as a normal pointer-to-function.
379 gdb_test "print diamond.*diamond_pfunc_ptr (20)" \
380 "Invalid data type for function to be called."
382 # With parentheses, it is valid.
384 gdb_test "print (diamond.*diamond_pfunc_ptr) (20)" \
387 # Make sure that we do not interpret this as either a member pointer
388 # call or a member function call.
390 gdb_test "print diamond.func_ptr (20)" \
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 \\*( const)?, int\\)\r\n$gdb_prompt $" {
404 -re "type = struct \{.*\}\r\n$gdb_prompt $" {
405 # gcc 2.95.3 -gdwarf-2
406 # gcc 2.95.3 -gstabs+
407 # gcc 3.2.2 -gdwarf-2
409 # gcc HEAD 2004-01-10 -gdwarf-2
410 # gcc HEAD 2004-01-10 -gstabs+
411 kfail "gdb/NNNN" $name
415 # print a pointer to a method
418 gdb_test_multiple "print pmf" $name {
419 -re "$vhn = \\(int \\(A::\\*\\)\\(A \\*( const)?, int\\)\\) $hex <A::bar\\(int\\)>\r\n$gdb_prompt $" {
422 -re "$vhn = \{.*\}\r\n$gdb_prompt $" {
423 # gcc 2.95.3 -gdwarf-2
424 # gcc 2.95.3 -gstabs+
425 # gcc 3.2.2 -gdwarf-2
427 # gcc HEAD 2004-01-10 -gdwarf-2
428 # gcc HEAD 2004-01-10 -gstabs+
429 kfail "gdb/NNNN" $name
433 # ptype a pointer to a pointer to a method
435 set name "ptype pmf_p"
436 gdb_test_multiple "ptype pmf_p" $name {
437 -re "type = int \\( ?A::\\*\\*\\)\\(A \\*( const)?, int\\)\r\n$gdb_prompt $" {
440 -re "type = struct \{.*\} \\*\r\n$gdb_prompt $" {
441 # gcc 2.95.3 -gdwarf-2
442 # gcc 2.95.3 -gstabs+
443 # gcc 3.2.2 -gdwarf-2
445 # gcc HEAD 2004-01-10 -gdwarf-2
446 # gcc HEAD 2004-01-10 -gstabs+
447 kfail "gdb/NNNN" $name
451 # print a pointer to a pointer to a method
453 set name "print pmf_p"
454 gdb_test_multiple "print pmf_p" $name {
455 -re "$vhn = \\(int \\( ?A::\\*\\*\\)\\)\\(int\\)\\) $hex\r\n$gdb_prompt $" {
458 -re "$vhn = \\(PMF \\*\\) $hex\r\n$gdb_prompt $" {
461 -re "$vhn = \\(struct \{.*\} \\*\\) $hex\r\n$gdb_prompt $" {
462 # gcc 2.95.3 -gdwarf-2
463 kfail "gdb/NNNN" $name
467 # print dereferenced pointer to method
469 set name "print a.*pmf"
470 gdb_test_multiple "print a.*pmf" $name {
471 -re "$vhn = {int \\(A \\*( const)?, int\\)} $hex <A::bar\\(int\\)>\r\n$gdb_prompt $" {
474 -re "Value can't be converted to integer.\r\n$gdb_prompt $" {
475 # gcc 2.95.3 -gdwarf-2
476 # gcc 2.95.3 -gstabs+
477 # gcc 3.2.2 -gdwarf-2
479 # gcc HEAD 2004-01-10 -gdwarf-2
480 # gcc HEAD 2004-01-10 -gstabs+
481 kfail "gdb/NNNN" $name
485 # print dereferenced pointer to method, using ->*
487 set name "print a_p->*pmf"
488 gdb_test_multiple "print a_p->*pmf" $name {
489 -re "$vhn = {int \\(A \\*( const)?, int\\)} $hex <A::bar\\(int\\)>\r\n$gdb_prompt $" {
492 -re "Value can't be converted to integer.\r\n$gdb_prompt $" {
493 # gcc 2.95.3 -gdwarf-2
494 # gcc 2.95.3 -gstabs+
495 # gcc 3.2.2 -gdwarf-2
497 # gcc HEAD 2004-01-10 -gdwarf-2
498 # gcc HEAD 2004-01-10 -gstabs+
499 kfail "gdb/NNNN" $name
503 # set the pointer to data member
505 set name "set var pmf = &A::foo"
506 gdb_test_multiple "set var pmf = &A::foo" $name {
507 -re "set var pmf = &A::foo\r\n$gdb_prompt $" {
508 # I have to match the echo'ed input explicitly here.
509 # If I leave it out, the pattern becomes too general
510 # and matches anything that ends in "$gdb_prompt $".
513 -re "Invalid cast.\r\n$gdb_prompt $" {
514 # gcc 2.95.3 -gdwarf-2
515 # gcc 2.95.3 -gstabs+
516 # gcc 3.2.2 -gdwarf-2
518 # gcc HEAD 2004-01-10 -gdwarf-2
519 # gcc HEAD 2004-01-10 -gstabs+
520 kfail "gdb/NNNN" $name
524 # dereference the pointer to data member without any object
525 # this is not allowed: a pmf must be bound to an object to dereference
527 set name "print *pmf"
528 gdb_test_multiple "print *pmf" $name {
529 -re "Attempt to dereference pointer to member without an object\r\n$gdb_prompt $" {
532 -re "Structure has no component named operator\\*.\r\n$gdb_prompt $" {
533 # gcc 2.95.3 -gdwarf-2
534 # gcc 2.95.3 -gstabs+
535 # gcc 3.3.2 -gdwarf-2
537 # gcc HEAD 2004-01-10 -gdwarf-2
538 # gcc HEAD 2004-01-10 -gstabs+
539 kfail "gdb/NNNN" $name
543 # dereference the pointer to data member without any object
544 # this is not allowed: a pmf must be bound to an object to dereference
546 set name "ptype *pmf"
547 gdb_test_multiple "ptype *pmf" $name {
548 -re "Attempt to dereference pointer to member without an object\r\n$gdb_prompt $" {
551 -re "Structure has no component named operator\\*.\r\n$gdb_prompt $" {
552 # gcc 2.95.3 -gdwarf-2
553 # gcc 2.95.3 -gstabs+
554 # gcc 3.3.2 -gdwarf-2
556 # gcc HEAD 2004-01-10 -gdwarf-2
557 # gcc HEAD 2004-01-10 -gstabs+
558 kfail "gdb/NNNN" $name
562 # Call a function through a pmf.
564 set name "print (a.*pmf)(3)"
565 gdb_test_multiple "print (a.*pmf)(3)" $name {
566 -re "$vhn = 50\r\n$gdb_prompt $" {
569 -re "Value can't be converted to integer.\r\n$gdb_prompt $" {
570 # gcc 2.95.3 -gdwarf-2
571 # gcc 2.95.3 -gstabs+
572 # gcc 3.3.2 -gdwarf-2
574 # gcc HEAD 2004-01-10 -gdwarf-2
575 # gcc HEAD 2004-01-10 -gstabs+
576 kfail "gdb/NNNN" $name
580 gdb_test "ptype a.*pmf" "type = int \\(A \\*( const)?, int\\)"
581 gdb_test "ptype (a.*pmf)(3)" "type = int"
583 # Print out a pointer to data member which requires looking into
585 gdb_test "print diamond_pmi" "$vhn = &Base::x"
586 gdb_test "print diamond.*diamond_pmi" "$vhn = 77"
588 # Examine some more complicated pmfs, which require adjusting "this"
589 # and looking through virtual tables.
591 # These two have a different object adjustment, but call the same method.
592 gdb_test "print diamond.*left_pmf" \
593 "$vhn = {int \\(Diamond \\*( const)?\\)} $hex <Base::get_x\\((void|)\\)>"
594 gdb_test "print diamond.*right_pmf" \
595 "$vhn = {int \\(Diamond \\*( const)?\\)} $hex <Base::get_x\\((void|)\\)>"
597 gdb_test "print (diamond.*left_pmf) ()" "$vhn = 77"
598 gdb_test "print (diamond.*right_pmf) ()" "$vhn = 88"
600 # These two point to different methods, although they have the same
601 # virtual table offsets.
602 gdb_test "print diamond.*left_vpmf" \
603 "$vhn = {int \\(Diamond \\*( const)?\\)} $hex <Left::vget\\((void|)\\)>"
604 gdb_test "print diamond.*right_vpmf" \
605 "$vhn = {int \\(Diamond \\*( const)?\\)} $hex <Right::vget\\((void|)\\)>"
607 gdb_test "print (diamond.*left_vpmf) ()" "$vhn = 177"
608 gdb_test "print (diamond.*left_base_vpmf) ()" "$vhn = 2077"
609 gdb_test "print (diamond.*right_vpmf) ()" "$vhn = 288"
611 # We should be able to figure out left_vpmf even without an object,
612 # because it comes from a non-virtual base. The same for right_vpmf.
613 gdb_test "print left_vpmf" "$vhn = &virtual Left::vget\\(\\)"
614 gdb_test "print right_vpmf" "$vhn = &virtual Right::vget\\(\\)"
616 # But we should gracefully fail to figure out base_vpmf, because
617 # its runtime type is more derived than its static type. This
618 # is a valid but unspecified cast (it is value preserving, i.e.
619 # can be casted back to the correct type and used).
620 gdb_test "print base_vpmf" \
621 "$vhn = &virtual table offset \[0-9\]*, this adjustment -\[0-9\]*"
623 # Make sure we parse this correctly; it's invalid.
624 gdb_test "print diamond.*left_vpmf ()" \
625 "Invalid data type for function to be called\\."
627 # NULL pointer to member tests.
628 gdb_test "print null_pmi" "$vhn = NULL"
629 gdb_test "print null_pmi = &A::j" "$vhn = &A::j"
630 gdb_test "print null_pmi = 0" "$vhn = NULL"
632 gdb_test "print null_pmf" "$vhn = NULL"
633 gdb_test "print null_pmf = &A::foo" "$vhn = \\(int \\(A::\\*\\)\\(A \\*( const)?, int\\)\\) $hex <A::foo ?\\(int\\)>"
634 gdb_test "print null_pmf = 0" "$vhn = NULL"