set test "pthread_stacks"

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

set exepath "[pwd]/pthread_stacks.x"
set flags "additional_flags=-pthread additional_flags=-g"

set res [target_compile $srcdir/$subdir/pthread_stacks.c $exepath executable $flags]
if { $res != "" } {
    fail "$test compiling"
    return
} else {
    pass "$test compiling"
}

# For ppc64, 0x10000 is too small. For x86_64, 0x20000 is invalid. So,
# we'll default to 0x10000 unless we're on ppc64.
if {[string match ppc* $::tcl_platform(machine)]} {
    set new_stack_size 0x20000
} else {
    set new_stack_size 0x10000
}

# This test won't work if we don't have glibc's debuginfo
# installed. So, try to compile threadstacks.stp first to see if we've
# got glibc debuginfo. Note that on multilib systems like x86_64, you
# only need the "native" glibc debuginfo installed for this test,
# since we're only compiling for the "native" arch.
set test "pthread_stacks debuginfo check"
spawn stap -gp4 $srcdir/systemtap.examples/process/threadstacks.stp \
    -Gsize=${new_stack_size} -d $exepath
set compile_errors 0
expect {
    -timeout 60
    -re {^parse error[^\r\n]*\r\n} { incr compile_errors 1; exp_continue }
    -re {^[^\r\n]*compilation failed[^\r\n]*\r\n} {
	incr compile_errors 1; exp_continue }
    -re {^semantic error[^\r\n]*\r\n} { incr compile_errors 1; exp_continue }
    timeout { fail "$test timeout" }
    eof { }
}
catch { close }
set res [wait -i $spawn_id]
set res [lindex $res 3]
if { $res != 0 || $compile_errors > 0 } {
    fail $test
    untested "pthread_stacks"
    catch {exec rm -f $exepath}
    return
}
pass $test

set test "pthread_stacks no -Gsize"
spawn stap -w -g $srcdir/systemtap.examples/process/threadstacks.stp -c "$exepath 1024 0" -d $exepath
set ok 0
set ko 0
expect {
    -re {^stacksize=[0-9]*\r\n} { incr ok; exp_continue }
    -re {^WARNING[^\r\n]*\r\n} { incr ko; exp_continue }
    timeout { fail "$test timeout" }
    eof { }
}
catch { close }
catch { wait }
if { $ok == 1 } then { pass $test } else { fail "$test ($ok $ko)" }


set test "pthread_stacks -Gsize"
spawn stap -g $srcdir/systemtap.examples/process/threadstacks.stp -Gsize=${new_stack_size} -c "$exepath 1024 0" -d $exepath
set ok 0
set ko 0
expect {
    -re {^stacksize=[0-9]*\r\n} { incr ok; exp_continue }
    -re {^pthread_stacks[^\r\n]*overwrote[^\r\n]*\r\n} { incr ok; exp_continue }
    -re {^WARNING[^\r\n]*\r\n} { incr ko; exp_continue }
    timeout { fail "$test timeout" }
    eof { }
}
catch { close }
catch { wait }
if { $ok == 2 } then { pass $test } else { fail "$test ($ok $ko)" }

catch {exec rm -f $exepath}
