gcc-gdb-test.exp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # Copyright (C) 2009-2015 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 GCC; see the file COPYING3. If not see
  14. # <http://www.gnu.org/licenses/>.
  15. # Utility for testing variable values using gdb, invoked via dg-final.
  16. # Call pass if variable has the desired value, otherwise fail.
  17. #
  18. # Argument 0 is the line number on which to put a breakpoint
  19. # Argument 1 is the name of the variable to be checked
  20. # possibly prefixed with type: to get the type of the variable
  21. # instead of the value of the variable (the default).
  22. # Argument 2 is the expected value (or type) of the variable
  23. # When asking for the value, the expected value is produced
  24. # calling print on it in gdb. When asking for the type it is
  25. # the literal string with extra whitespace removed.
  26. # Argument 3 handles expected failures and the like
  27. proc gdb-test { args } {
  28. if { ![isnative] || [is_remote target] } { return }
  29. if { [llength $args] >= 4 } {
  30. switch [dg-process-target [lindex $args 3]] {
  31. "S" { }
  32. "N" { return }
  33. "F" { setup_xfail "*-*-*" }
  34. "P" { }
  35. }
  36. }
  37. # This assumes that we are three frames down from dg-test, and that
  38. # it still stores the filename of the testcase in a local variable "name".
  39. # A cleaner solution would require a new DejaGnu release.
  40. upvar 2 name testcase
  41. upvar 2 prog prog
  42. # The command to run on the variable
  43. set arg1 [lindex $args 1]
  44. if { [string equal -length 5 "type:" $arg1] == 1 } {
  45. set command "ptype"
  46. set var [string range $arg1 5 end]
  47. } else {
  48. set command "print"
  49. set var $arg1
  50. }
  51. set gdb_name $::env(GUALITY_GDB_NAME)
  52. set testname "$testcase line [lindex $args 0] [lindex $args 1] == [lindex $args 2]"
  53. set output_file "[file rootname [file tail $prog]].exe"
  54. set cmd_file "[file rootname [file tail $prog]].gdb"
  55. set fd [open $cmd_file "w"]
  56. puts $fd "break [lindex $args 0]"
  57. puts $fd "run"
  58. puts $fd "$command $var"
  59. if { $command == "print" } {
  60. # For values, let gdb interpret them by printing them.
  61. puts $fd "print [lindex $args 2]"
  62. } else {
  63. # Since types can span multiple lines, we need an end marker.
  64. puts $fd "echo TYPE_END\\n"
  65. }
  66. puts $fd "quit"
  67. close $fd
  68. send_log "Spawning: $gdb_name -nx -nw -quiet -x $cmd_file ./$output_file\n"
  69. set res [remote_spawn target "$gdb_name -nx -nw -quiet -x $cmd_file ./$output_file"]
  70. if { $res < 0 || $res == "" } {
  71. unsupported "$testname"
  72. file delete $cmd_file
  73. return
  74. }
  75. remote_expect target [timeout_value] {
  76. # Too old GDB
  77. -re "Unhandled dwarf expression|Error in sourced command file|<unknown type in " {
  78. unsupported "$testname"
  79. remote_close target
  80. file delete $cmd_file
  81. return
  82. }
  83. # print var; print expected
  84. -re {[\n\r]\$1 = ([^\n\r]*)[\n\r]+\$2 = ([^\n\r]*)[\n\r]} {
  85. set first $expect_out(1,string)
  86. set second $expect_out(2,string)
  87. if { $first == $second } {
  88. pass "$testname"
  89. } else {
  90. # We need the -- to disambiguate $first from an option,
  91. # as it may be negative.
  92. send_log -- "$first != $second\n"
  93. fail "$testname"
  94. }
  95. remote_close target
  96. file delete $cmd_file
  97. return
  98. }
  99. # ptype var;
  100. -re {[\n\r]type = (.*)[\n\r][\n\r]TYPE_END[\n\r]} {
  101. set type $expect_out(1,string)
  102. # Squash all extra whitespace/newlines that gdb might use for
  103. # "pretty printing" into one so result is just one line.
  104. regsub -all {[\n\r\t ]+} $type " " type
  105. # Old gdb might output "long int" instead of just "long"
  106. # and "short int" instead of just "short". Canonicalize.
  107. regsub -all {\mlong int\M} $type "long" type
  108. regsub -all {\mshort int\M} $type "short" type
  109. set expected [lindex $args 2]
  110. if { $type == $expected } {
  111. pass "$testname"
  112. } else {
  113. send_log -- "$type != $expected\n"
  114. fail "$testname"
  115. }
  116. remote_close target
  117. file delete $cmd_file
  118. return
  119. }
  120. timeout {
  121. unsupported "$testname"
  122. remote_close target
  123. file delete $cmd_file
  124. return
  125. }
  126. }
  127. unsupported "$testname"
  128. remote_close target
  129. file delete $cmd_file
  130. return
  131. }