# test end probe creation and display of written and unread global variables

set test "global_end"

if {![installtest_p]} {untested $test; return}

foreach private {0 1} {
foreach runtime [get_runtime_list] {
    set test "global_end"

    if {$runtime != ""} {
	set test "${test}_${runtime}"
	spawn sh -c "stap $srcdir/$subdir/global_end.stp $private --runtime=$runtime | sort"
    } else {
	spawn sh -c "stap $srcdir/$subdir/global_end.stp $private | sort"
    }
    set ok 0

    set ok 0
    expect {
	-timeout 180
	-re {^alpha."two".2.=0x4\r\n} { incr ok; exp_continue }
	-re {^alpha."two".1.=0x3\r\n} { incr ok; exp_continue }
	-re {^alpha."one".2.=0x2\r\n} { incr ok; exp_continue }
	-re {^alpha."one".1.=0x1\r\n} { incr ok; exp_continue }
	-re {^epsilon."one", 1. count:4 min:1 max:4 sum:10 avg:2\r\n} {
	    incr ok; exp_continue }
	-re {^epsilon."two", 2. count:4 min:10 max:40 sum:100 avg:25\r\n} {
	    incr ok; exp_continue }
	-re {^gamma="abcdefghijklmnopqrstuvwxyz"\r\n} { incr ok; exp_continue }
	-re {^iota."two".="twelve"\r\n} { incr ok; exp_continue }
	-re {^iota."one".="eleven"\r\n} { incr ok; exp_continue }
	-re {^.one,0x1.=0x1 .one,0x2.=0x2 .two,0x1.=0x3 .two,0x2.=0x4 \r\n} { incr ok; exp_continue }
	-re {^phi @count=4 @min=1 @max=4 @sum=10 @avg=2\r\n} {
	    incr ok; exp_continue }
	timeout { fail "$test (timeout)" }
	eof { }
    }
    catch {close}; catch {wait}

    if {$runtime != ""} {
	spawn stap $srcdir/$subdir/global_end2.stp --runtime=$runtime
    } else {
	spawn stap $srcdir/$subdir/global_end2.stp
    }
    # above should not do any tapset global variable display so
    # ok should only change by 1
    expect {
	-timeout 180
	-re {Avg time = [0-9]} { incr ok; exp_continue }
    }
    catch {close}; catch {wait}
    
    if {$ok == 12} { pass "$test ($ok)" } { fail "$test ($ok)" }
    
    # test PR6954
    set test "global_end_var"
    if {$runtime != ""} {
	set test "${test}_${runtime}"
	spawn stap $srcdir/$subdir/global_end_var.stp $private --runtime=$runtime
    } else {
	spawn stap $srcdir/$subdir/global_end_var.stp $private
    }
    set ok 0
    expect {
	-timeout 180
	-re {gs0_save=0x29} { incr ok; exp_continue }
	-re {gs2=0x4} { incr ok; exp_continue }
	-re {gs3=0x4} { incr ok; exp_continue }
	-re {gs4=0x5} { incr ok; exp_continue }
	-re {ga1."foo".=0x2} { incr ok; exp_continue }
	-re {ga2."foo".=0x1} { incr ok; exp_continue }
	timeout { fail "$test (timeout)" }
	eof { }
    }
    catch {close}; catch {wait}
    
    if {$ok == 6} { pass "$test ($ok)" } { fail "$test ($ok)" }

    # test PR17749
    set test "global_end_var_used"
    if {$runtime != ""} {
	set test "${test}_${runtime}"
	spawn stap $srcdir/$subdir/global_end_var_used.stp $private --runtime=$runtime
    } else {
	spawn stap $srcdir/$subdir/global_end_var_used.stp $private
    }
    set ok 0
    # strictly look for "hello". if anything else is printed it's a fail.
    expect {
	-timeout 180
        -re {^hello!$} { incr ok; exp_continue }
	timeout { fail "$test (timeout)" }
	eof { }
    }
    catch {close}; catch {wait}

    if {$ok == 1} { pass "$test ($ok)" } { fail "$test ($ok)" }
}
}
