# alternatives.exp
#
# 2 simple tests:
#
# LOCAL1: makes sure that when a probe is compiled that accesses a
# non-existent local variable, the local variables that do exist are
# listed as alternatives.
#
# STRUCT1: makes sure that when a probe is compiled that accesses a
# non-existent structure member, the structure members that do exist
# are listed as alternatives.
#
# Note that the tests don't check if the correct alternatives are
# listed, but that some alternatives are listed.

set local1_script {
    probe kernel.function("vfs_write") { ret = $z; }
}

set struct1_script {
    probe kernel.function("vfs_write") { f_pos = $file->f_po; }
}

proc stap_run_alternatives {args} {
    set alternatives_found 0
    verbose -log "starting $args"
    eval spawn $args
    expect { 
	-timeout 60
	-re {semantic error: .+ \(alternatives: \$?[a-zA-Z_]} {incr alternatives_found; exp_continue}
        -re {[^\r]*\r} { verbose -log $expect_out(0,string); exp_continue } 
        eof { verbose -log "EOF" }
        timeout { verbose -log "TIMEOUT" }
    }
    set results [wait]
    verbose -log "wait results: $results"
    return $alternatives_found
}

set test "LOCAL1"
set rc [stap_run_alternatives stap -u -p2 -e $local1_script]
if {$rc >= 1} { pass $test } else { fail "$test ($rc)" }

set test "STRUCT1"
set rc [stap_run_alternatives stap -u -p2 -e $struct1_script]
if {$rc >= 1} { pass $test } else { fail "$test ($rc)" }
