implref-array.exp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. # Copyright 2016-2022 Free Software Foundation, Inc.
  2. # This program is free software; you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation; either version 3 of the License, or
  5. # (at your option) any later version.
  6. #
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. # Test a C++ reference marked with DW_OP_GNU_implicit_pointer.
  15. # The referenced value is a global array whose location is a DW_OP_addr.
  16. if [skip_cplus_tests] {
  17. continue
  18. }
  19. load_lib dwarf.exp
  20. # This test can only be run on targets which support DWARF-2 and use gas.
  21. if ![dwarf2_support] {
  22. return 0
  23. }
  24. # We'll place the output of Dwarf::assemble in implref-array.S.
  25. standard_testfile .c .S
  26. # ${testfile} is now "implref-array". srcfile2 is "implref-array.S".
  27. set executable ${testfile}
  28. set asm_file [standard_output_file ${srcfile2}]
  29. # We need to know the size of integer and address types in order
  30. # to write some of the debugging info we'd like to generate.
  31. #
  32. # For that, we ask GDB by debugging our implref-array program.
  33. # Any program would do, but since we already have implref-array
  34. # specifically for this testcase, might as well use that.
  35. if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } {
  36. return -1
  37. }
  38. set array_length [get_valueof "/u" "sizeof(array) / sizeof(array\[0\])" -1]
  39. # Create the DWARF. We need a regular variable which represents the array, and
  40. # a reference to it that'll be marked with DW_OP_GNU_implicit_pointer.
  41. # The variable must be global so that its name is an exported symbol that we
  42. # can reference from the DWARF using gdb_target_symbol.
  43. Dwarf::assemble ${asm_file} {
  44. global array_length
  45. cu {} {
  46. DW_TAG_compile_unit {
  47. {DW_AT_language @DW_LANG_C_plus_plus}
  48. } {
  49. declare_labels int_label sizetype_label array_label variable_label ref_label
  50. set int_size [get_sizeof "int" -1]
  51. set upper_bound [expr ${array_length} - 1]
  52. # gdb always assumes references are implemented as pointers.
  53. set addr_size [get_sizeof "void *" -1]
  54. int_label: DW_TAG_base_type {
  55. {DW_AT_byte_size ${int_size} DW_FORM_udata}
  56. {DW_AT_encoding @DW_ATE_signed}
  57. {DW_AT_name "int"}
  58. }
  59. sizetype_label: DW_TAG_base_type {
  60. {DW_AT_byte_size ${int_size} DW_FORM_udata}
  61. {DW_AT_encoding @DW_ATE_unsigned}
  62. {DW_AT_name "sizetype"}
  63. }
  64. array_label: DW_TAG_array_type {
  65. {DW_AT_type :${int_label}}
  66. } {
  67. DW_TAG_subrange_type {
  68. {DW_AT_type :${sizetype_label}}
  69. {DW_AT_lower_bound 0 DW_FORM_udata}
  70. {DW_AT_upper_bound ${upper_bound} DW_FORM_udata}
  71. }
  72. }
  73. ref_label: DW_TAG_reference_type {
  74. {DW_AT_byte_size ${addr_size} DW_FORM_udata}
  75. {DW_AT_type :${array_label}}
  76. }
  77. variable_label: DW_TAG_variable {
  78. {DW_AT_name "array"}
  79. {DW_AT_type :${array_label}}
  80. {DW_AT_external 1 DW_FORM_flag}
  81. {DW_AT_location {DW_OP_addr [gdb_target_symbol "array"]} SPECIAL_expr}
  82. }
  83. DW_TAG_subprogram {
  84. {MACRO_AT_func { "main" }}
  85. {DW_AT_type :${int_label}}
  86. {DW_AT_external 1 DW_FORM_flag}
  87. } {
  88. DW_TAG_variable {
  89. {DW_AT_name "ref"}
  90. {DW_AT_type :${ref_label}}
  91. {DW_AT_location {DW_OP_GNU_implicit_pointer ${variable_label} 0} SPECIAL_expr}
  92. }
  93. }
  94. }
  95. }
  96. }
  97. if [prepare_for_testing "failed to prepare" ${executable} [list ${asm_file} ${srcfile}] {}] {
  98. return -1
  99. }
  100. # DW_OP_GNU_implicit_pointer implementation requires a valid frame.
  101. if ![runto_main] {
  102. return -1
  103. }
  104. # This matches e.g. '(int (&)[5])'
  105. set ref_type [format {\(int \(&\)\[%d\]\)} ${array_length}]
  106. # This matches e.g. '(int (*)[5])'
  107. set ptr_type [format {\(int \(\*\)\[%d\]\)} ${array_length}]
  108. # Contents of the array. Trim leading/trailing whitespace, '{' and '}'
  109. # since they confuse TCL to no end.
  110. set contents [get_valueof "" "array" ""]
  111. set contents [string trim ${contents}]
  112. set contents [string trim ${contents} "{}"]
  113. # Address of the referenced value.
  114. set address [get_hexadecimal_valueof "&array" ""]
  115. # Doing 'print ref' should show us e.g. '(int (&)[5]) 0xdeadbeef: {0, 1, 2, 3, 4}'.
  116. gdb_test "print ref" " = ${ref_type} @${address}: \\{${contents}\\}"
  117. # Doing 'print &ref' should show us e.g. '(int (*)[5]) 0xdeadbeef <array>'.
  118. gdb_test "print &ref" " = ${ptr_type} ${address} <array>"
  119. # gdb assumes C++ references are implemented as pointers, and print &(&ref)
  120. # shows us the underlying pointer's address. Since in this case there's no
  121. # physical pointer, gdb should tell us so.
  122. gdb_test "print &(&ref)" "Attempt to take address of value not located in memory."
  123. # Test assignment through the synthetic reference.
  124. set first_value 10
  125. gdb_test_no_output "set (ref\[0\] = ${first_value})"
  126. # This matches '{10, 1, 2, 3, 4}'.
  127. set new_contents [format {\{%d, 1, 2, 3, 4\}} ${first_value}]
  128. # Doing 'print ref' should now show us e.g.
  129. # '(int (&)[5]) <synthetic pointer>: {10, 1, 2, 3, 4}'.
  130. gdb_test "print ref" " = ${ref_type} @${address}: ${new_contents}" "print ref after assignment"
  131. gdb_test "print array" " = ${new_contents}" "print array after assignment"
  132. # Test treating the array as a pointer.
  133. set second_value 20
  134. set new_contents [format {\{%d, %d, 2, 3, 4\}} ${first_value} ${second_value}]
  135. gdb_test "print *ref" " = ${first_value}"
  136. gdb_test_no_output "set (*(ref + 1) = ${second_value})"
  137. gdb_test "print ref\[1\]" " = ${second_value}"
  138. gdb_test "print array" " = ${new_contents}" "print array after second assignment"