#! stap -p2


@define STUFF %(
    // precheck, it should work with @cast
    if (!@defined(@task(0)->mm)) {
        println($cast_failed_mm)
    }
    if (@defined(@task(0)->systemtap)) {
        println($cast_succeeded_systemtap)
    }

    // Test that autocast values can be checked by @defined
    if (!@defined(task_current()->mm)) {
        println($autocast_failed_mm)
    }
    if (@defined(task_current()->systemtap)) {
        println($autocast_succeeded_systemtap)
    }

    // Test that autocast can resolve on the results of @defined
    mm1 = @choose_defined($nonsense, task_current())->mm;
    mm2 = @choose_defined(task_current(), $nonsense)->mm;
    println(mm1 == mm2)

    // Test an even deeper level of @defined
    if (!@defined(mm1->mmap) || !@defined(mm2->mmap)) {
        println($autocast_failed_mm_mmap)
    }
    if (@defined(mm1->systemtap) || @defined(mm2->systemtap)) {
        println($autocast_succeeded_mm_systemtap)
    }

    // Test that autocast can resolve through nested @defined
    // (especially that the ternary isn't automatically "defined")
    mm3 = @choose_defined(@choose_defined($nonsense, $wut), task_current())->mm;
    mm4 = @choose_defined(@choose_defined($nonsense, task_current()), $wut)->mm;
    mm5 = @choose_defined(@choose_defined(task_current(), $nonsense), $wut)->mm;
    println(mm3 == mm4 && mm4 == mm5)
%)


probe oneshot
{
  @STUFF
  foo() // from a function too, to test PR18079 function processing
}

function foo ()
{
  @STUFF
}
