set test "abort"
set testpath "$srcdir/$subdir"

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

# --- TEST 1 ---

set subtest1 "TEST 1: abort() in the middle of a func body"
foreach runtime [get_runtime_list] {
    if {$runtime eq ""} {
        set runtime "kernel"
    }
    set test_name "$test: $subtest1 ($runtime)"

    set exit_code [run_cmd_2way "stap --runtime=$runtime '$srcdir/$subdir/${test}_1.stp'" out stderr]

    set exp_out "enter f
"
    is "${test_name}: stdout" $out $exp_out

    is "${test_name}: exit code" $exit_code 0
    if {$stderr ne ""} {
        send_log "stderr:\n$stderr"
    }
}

# --- TEST 2 ---

set subtest2 "TEST 2: abort() in the middle of a func body in a deeper func call chain"
foreach runtime [get_runtime_list] {
    if {$runtime eq ""} {
        set runtime "kernel"
    }
    set test_name "$test: $subtest2 ($runtime)"

    set exit_code [run_cmd_2way "stap --runtime=$runtime '$srcdir/$subdir/${test}_2.stp'" out stderr]

    set exp_out "enter g
enter f
"
    is "${test_name}: stdout" $out $exp_out

    is "${test_name}: exit code" $exit_code 0
    if {$stderr ne ""} {
        send_log "stderr:\n$stderr"
    }
}

# --- TEST 3 ---

set subtest3 "TEST 3: abort() in the middle of a probe handler body"
foreach runtime [get_runtime_list] {
    if {$runtime eq ""} {
        set runtime "kernel"
    }
    set test_name "$test: $subtest3 ($runtime)"

    set exit_code [run_cmd_2way "stap --runtime=$runtime '$srcdir/$subdir/${test}_3.stp'" out stderr]

    set exp_out "enter probe
"
    is "${test_name}: stdout" $out $exp_out

    is "${test_name}: exit code" $exit_code 0
    if {$stderr ne ""} {
        send_log "stderr:\n$stderr"
    }
}

# --- TEST 4 ---

set subtest4 "TEST 4: abort() in the middle of a probe handler body (--compatible 4.0)"
foreach runtime [get_runtime_list] {
    if {$runtime eq ""} {
        set runtime "kernel"
    }
    set test_name "$test: $subtest4 ($runtime)"

    set exit_code [run_cmd_2way "stap --compatible 4.0 --runtime=$runtime '$srcdir/$subdir/${test}_3.stp'" out stderr]

    set exp_out "enter probe
"
    is "${test_name}: stdout" $out $exp_out

    is "${test_name}: exit code" $exit_code 0
    if {$stderr ne ""} {
        send_log "stderr:\n$stderr"
    }
}

# --- TEST 5 ---

set subtest5 "TEST 5: abort() in the middle of a probe handler body (--compatible 3.3)"
set test_name "$test: $subtest5"

set exit_code [run_cmd_2way "stap --compatible 3.3 '$srcdir/$subdir/${test}_3.stp'" out stderr]

set exp_out ""
is "${test_name}: stdout" $out $exp_out
isnt "${test_name}: exit code" $exit_code 0

set stderr_pat "^semantic error: unresolved function.*\\(similar: \[^\\n\]*?\\): identifier 'abort' at \[^\\n\]*?\\.stp:3:5\\n"
like "${test_name}: stderr" $stderr $stderr_pat "-linestop -lineanchor"

# --- TEST 6 ---

set subtest6 "TEST 6: abort() in timer.profile (using globals)"
set test_name "$test: $subtest6"

set exit_code [run_cmd_2way "stap '$srcdir/$subdir/${test}_6.stp'" out stderr]

set exp_out "fire 3!
fire 2!
fire 1!
"
is "${test_name}: stdout" $out $exp_out

is "${test_name}: exit code" $exit_code 0
if {$stderr ne ""} {
    send_log "stderr:\n$stderr"
}

# --- TEST 7 ---

set subtest7 "TEST 7: abort() in timer.profile (more concurrency and no globals)"
set test_name "$test: $subtest7"

set exit_code [run_cmd_2way "stap '$srcdir/$subdir/${test}_7.stp'" out stderr]

set exp_out ""
is "${test_name}: stdout" $out $exp_out

set exp_stderr ""
is "${test_name}: stderr" $stderr $exp_stderr

is "${test_name}: exit code" $exit_code 0

# --- TEST 8 ---

set subtest8 "TEST 8: abort() in the middle of a func body - abort() cannot be caught"
foreach runtime [get_runtime_list] {
    if {$runtime eq ""} {
        set runtime "kernel"
    }
    set test_name "$test: $subtest8 ($runtime)"

    set exit_code [run_cmd_2way "stap --runtime=$runtime '$srcdir/$subdir/${test}_8.stp'" out stderr]

    set exp_out "enter f
"
    is "${test_name}: stdout" $out $exp_out

    is "${test_name}: exit code" $exit_code 0
    if {$stderr ne ""} {
        send_log "stderr:\n$stderr"
    }
}
