Home | History | Annotate | Line # | Download | only in lib
      1 # Copyright 2015-2024 Free Software Foundation, Inc.
      2 
      3 # This program is free software; you can redistribute it and/or modify
      4 # it under the terms of the GNU General Public License as published by
      5 # the Free Software Foundation; either version 3 of the License, or
      6 # (at your option) any later version.
      7 #
      8 # This program is distributed in the hope that it will be useful,
      9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
     10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     11 # GNU General Public License for more details.
     12 #
     13 # You should have received a copy of the GNU General Public License
     14 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
     15 
     16 # Generic/oft used support routines for testing GDB's compile feature.
     17 
     18 # Helper function for skip_compile_feature_tests.  This does the real
     19 # work, but should not be called directly.  Returns a failure reason
     20 # (a string) on failure, or the empty string on success.
     21 
     22 proc _do_check_compile {expr} {
     23     global gdb_prompt
     24 
     25     set result ""
     26     gdb_test_multiple "compile code -- $expr;" "check for working compile command" {
     27 	"Could not load libcc1.*\r\n$gdb_prompt $" {
     28 	    set result "could not find libcc1"
     29 	}
     30 	"Could not load libcp1.*\r\n$gdb_prompt $" {
     31 	    set result "could not find libcp1"
     32 	}
     33 	-re "WARNING .* there are active plugins, do not report this" {
     34 	    # Note that the regexp above does not check for the
     35 	    # prompt.  This avoids a gratuitous timeout.
     36 	    set result "GCC crashed"
     37 	}
     38 	-re "confused by earlier errors, bailing out" {
     39 	    # This scenario can happen when either GCC or GDB is
     40 	    # confused by some other debuginfo.
     41 	    # See PR compile/29541.
     42 	    set result "confused by glibc debuginfo"
     43 	}
     44 	-re "$::decimal symbols were missing, cannot continue" {
     45 	    # This appears to be a bug in the compiler plugin.
     46 	    set result "apparent compiler plugin bug"
     47 	}
     48 	-re "\r\n$gdb_prompt $" {
     49 	}
     50     }
     51     return $result
     52 }
     53 
     54 # Return 1 if we should skip tests of the "compile" feature.
     55 # This must be invoked after the inferior has been started.
     56 # EXPR is the expression to test, if any (using the default empty EXPR
     57 # works fine in most cases).
     58 
     59 proc skip_compile_feature_tests {{expr ""}} {
     60     return [expr {[string length [_do_check_compile $expr]] > 0}]
     61 }
     62 
     63 # Like skip_compile_feature_tests, but also issue an "untested" when
     64 # skipping.
     65 
     66 proc skip_compile_feature_untested {{expr ""}} {
     67     set output [_do_check_compile $expr]
     68     if {[string length $output] > 0} {
     69 	untested "compile command not supported ($output)"
     70 	return 1
     71     }
     72     return 0
     73 }
     74 
     75 # This namespace provides some convenience functions for running
     76 # "compile code" and "compile print" tests.
     77 #
     78 # Exported functions are defined inline below.
     79 #
     80 # General usage:
     81 #
     82 # Start a new session, noting that the variable "var" will be used for
     83 # "compile code" expressions.  This variable /must/ exist in the stopped
     84 # location.
     85 #
     86 # CompileExpression::new "var"
     87 #
     88 # Test the implicit expression "foo;" with result/value 3.
     89 # CompileExpression::test "foo" 3
     90 # ---> Runs the following tests (name of tests ignored for illustration)
     91 #    gdb_test_no_output "compile code var = foo"
     92 #    gdb_test "p var" "= 3"
     93 #    gdb_test "compile print foo;" "= 3"
     94 #
     95 # Test the explicit expression "a = function (3); var = a;" with the result 21.
     96 # CompileExpression::test "a = function (3); var = a;" 21 -explicit
     97 # ---> Runs the following tests (name of tests ignored for illustration)
     98 #    gdb_test_no_output "compile code a = function (3); var = a;"
     99 #    gdb_test "p var" "= 21"
    100 #
    101 # Additional option flags may be passed to test to control the behavior
    102 # of the test harness:
    103 #
    104 # Pass -explicit to specify that the test uses an explicit expression,
    105 # one which sets the value of the variable (see above).  Only the code test
    106 # will be run.
    107 #
    108 # Pass -value and/or -print to indicate that the value and/or print steps
    109 # will optionally fail. Specify "xfail" or "kfail" to indicate how
    110 # particular step will fail.  These may be followed by any accepted DejaGNU
    111 # parameters such as architecture and bug#.  [See examples below.]
    112 #
    113 # To specify that the compile (and consequently print and value tests) is
    114 # expected to kfail/xfail, use -kfail or -xfail with any appropriate
    115 # DejaGNU parameters.  Both options override -print and -value.
    116 # [-xfail is given precedence over -kfail should both be given.]
    117 #
    118 # -value is used when a "code" test is run, specifying that the "compile
    119 # code" and "print VAR" steps will fail in the prescribed manner.
    120 # [If the print step generates a PASS, the test is considered invalidly
    121 # written.  VAR's value should /always/ be invalidated before a test is
    122 # run.]
    123 #
    124 # -print is used to specify that an expression will fail in the prescribed
    125 # manner when "print" test is executed.
    126 #
    127 # Pass "-name NAME" to set an optional test name.  If not specified,
    128 # the harness will use test names such as "compile code EXPR" and
    129 # "result of compile code EXPR".
    130 #
    131 # Pass "-noprint" or "-nocode" to suppress print or code tests, respectively,
    132 # This is useful when the expression being tested modifies the object
    133 # being tested, e.g., "a++".
    134 #
    135 # These options must be passed LAST to CompileExpression::test.
    136 #
    137 # Examples:
    138 #
    139 # Both "code" and "print" tests are expected to xfail:
    140 # CompileExpression add_imp "foo" 3 -compile {xfail *-*-*} -print {xfail *-*-*}
    141 #
    142 # The "print $VARIABLE" portion of the "code" test is expected to kfail
    143 # (the actual "compile code" GDB command will succeed), but the "print"
    144 # test should pass:
    145 # CompileExpression add_imp "foo" 3 -value {kfail *-*-* gdb/1234}
    146 
    147 namespace eval ::CompileExpression {
    148 
    149     # The variable name to check testing results.  This variable
    150     # must be in scope when tests are run.
    151     variable varName_ {}
    152 
    153     # Start a new expression list.  VARNAME is the name of the variable
    154     # that will be printed to check if the result of the test was
    155     # successful.
    156     proc new {varname} {
    157 	variable varName_
    158 
    159 	set varName_ $varname
    160     }
    161 
    162     # Test an expression.
    163     #
    164     # See the preamble for a list of valid optional arguments.
    165     #
    166     # Implicit expressions will be sent to GDB in the form
    167     # "$varName = $EXP".  "p $varName" will be used to decide the pass
    168     # or fail status of the test.
    169     #
    170     # Explicit expressions will be sent to GDB as-is and tested using only
    171     # "compile code".  The expression should set the value of the variable
    172     # $varName, which is then printed to determine whether the test passed
    173     # or failed.
    174     #
    175     # Unlike explicit expressions, implicit expressions are tested with both
    176     # "compile print" and "compile code".
    177 
    178     proc test {exp result args} {
    179 	parse_args {{value {"" ""}} {print {"" ""}} {name ""}
    180 	    {noprint} {nocode} {explicit} {xfail {"" ""}} {kfail {"" ""}}}
    181 
    182 	if {[lindex $xfail 0] != ""} {
    183 	    set l "xfail $xfail"
    184 	} elseif {[lindex $kfail 0] != ""} {
    185 	    set l "kfail $kfail"
    186 	} else {
    187 	    set l ""
    188 	    set compile {"" ""}
    189 	}
    190 	if {$l != ""} {
    191 	    set compile $l
    192 	    set print $l
    193 	    set value $l
    194 	}
    195 
    196 	set ok 1
    197 	if {!$nocode} {
    198 	    if {![do_test_ code $exp $result $explicit $name \
    199 		      [list $compile $value $print]]} {
    200 		set ok 0
    201 	    }
    202 	}
    203 	if {$ok && !$noprint} {
    204 	    if {![do_test_ print $exp $result $explicit $name \
    205 		      [list $compile $value $print]]} {
    206 		set ok 0
    207 	    }
    208 	}
    209 	if {!$ok} {
    210 	    return -code return 0
    211 	}
    212     }
    213 
    214     # Invoke a 'compile' command of some form.  COMMAND is the
    215     # command, RESULT is the expected output, and NAME is the test
    216     # name.  Issues a pass or fail.  Returns 1 on success, 0 if there
    217     # is a failure that should result in the entire remaining .exp
    218     # being stopped; in this case an 'unsupported' is issued.
    219 
    220     proc compile_command_ {command result name} {
    221 	global gdb_prompt
    222 	set this_result 1
    223 	gdb_test_multiple $command $name {
    224 	    -re "WARNING .* there are active plugins, do not report this" {
    225 		# Note that the regexp above does not check for the
    226 		# prompt.  This avoids a gratuitous timeout.
    227 		unsupported "GCC compiler plugin crashed"
    228 		set this_result 0
    229 	    }
    230 	    -re "$::decimal symbols were missing, cannot continue" {
    231 		# This appears to be a bug in the compiler plugin.
    232 		unsupported "GCC compiler plugin bug"
    233 		set this_result 0
    234 	    }
    235 	    -re -wrap "$result" {
    236 		pass $name
    237 	    }
    238 	}
    239 	return $this_result
    240     }
    241 
    242     # Run a compile test for CMD ("print" or "code").
    243     # Return 1 on success, 0 if there is some kind of catastrophic
    244     # error.
    245 
    246     proc do_test_ {cmd exp result is_explicit tst fail_list} {
    247 	variable varName_
    248 
    249 	if {![string match $cmd "code"]
    250 	    && ![string match $cmd "print"]} {
    251 	    error "invalid command, $cmd; should be \"print\" or \"compile\""
    252 	}
    253 
    254 	# Get expected result of test.  Will be "" if test is
    255 	# expected to PASS.
    256 	lassign $fail_list fail_compile fail_value fail_print
    257 
    258 	# Set a test name if one hasn't been provided.
    259 	if {$tst == ""} {
    260 	    set tst "compile $cmd $exp"
    261 	}
    262 
    263 	if {[string match $cmd "print"]} {
    264 	    if {!$is_explicit} {
    265 		eval setup_failures_ $fail_print
    266 		return [compile_command_ "compile print $exp" $result $tst]
    267 	    }
    268 	} else {
    269 	    if {$is_explicit} {
    270 		set command "compile code $exp"
    271 	    } else {
    272 		set command "compile code $varName_ = $exp"
    273 	    }
    274 	    eval setup_failures_ $fail_compile
    275 	    if {![compile_command_ $command "" $tst]} {
    276 		return 0
    277 	    }
    278 	    eval setup_failures_ $fail_value
    279 	    gdb_test "p $varName_" "= $result" "result of $tst"
    280 	}
    281 	return 1
    282     }
    283 
    284     # A convenience proc used to set up xfail and kfail tests.
    285     # HOW is either xfail or kfail (case is ignored).  ARGS is any
    286     # optional architecture, bug number, or other string to pass to
    287     # respective DejaGNU setup_$how routines.
    288 
    289     proc setup_failures_ {how args} {
    290 	switch -nocase $how {
    291 	    xfail {
    292 		eval setup_xfail $args
    293 	    }
    294 
    295 	    kfail {
    296 		eval setup_kfail $args
    297 	    }
    298 
    299 	    default {
    300 		# Do nothing.  Either the test is expected to PASS
    301 		# or we have an unhandled failure mode.
    302 	    }
    303 	}
    304     }
    305 }
    306