# Utrace system call argument tests.

set flags ""
set srcpath "$srcdir/$subdir/utrace_syscall_args.c"
set exepath "[pwd]/utrace_syscall_args"
set stppath "$srcdir/$subdir/utrace_syscall_args.stp"

set output_string "mmap\\(\[0-9\]+\\)\\(0x\[0-9a-f]+, 0x\[0-9a-f]+, 0x\[0-9a-f]+, 0x\[0-9a-f]+, 0x\[0-9a-f]+, 0x\[0-9a-f]+\\) = 0x\[0-9a-f]+\r\nmunmap\\(\[0-9\]+\\)\\(0x\[0-9a-f]+, 0x\[0-9a-f]+\\) = 0x\[0-9a-f]+\r\nclose\\(\[0-9\]+\\)\\(0x\[0-9a-f]+\\) = 0x\[0-9a-f]+\r\ndup\\(\[0-9\]+\\)\\(0x\[0-9a-f]+, 0x\[0-9a-f]+, 0x\[0-9a-f]+, 0x\[0-9a-f]+, 0x\[0-9a-f]+, 0x\[0-9a-f]+\\) = 0x\[0-9a-f]+\r\nbad_syscall\\(-?\[0-9\]+\\)\\(0x\[0-9a-f]+, 0x\[0-9a-f]+, 0x\[0-9a-f]+, 0x\[0-9a-f]+, 0x\[0-9a-f]+, 0x\[0-9a-f]+\\) = 0x\[0-9a-f]+\r\nsystemtap test success\r\n"

# For first pass, force 64-bit compilation for 64-bit systems.  Add
# any other 64-bit architecture you want tested below.
#
# To find tcl's platform name for your machine, run the following:
# echo "puts $::tcl_platform(machine)" | tclsh

switch -regexp $::tcl_platform(machine) {
    ^ia64$ {
	set do_64_bit_pass 1
	set flags "" 
    }
    ^(x86_64|ppc64|s390x)$ { 
	set do_64_bit_pass 1
	set flags "additional_flags=-m64"
    }
    default {
	set do_64_bit_pass 0
    }
}

if {$do_64_bit_pass} {
    set testname "64_BIT_UTRACE_SYSCALL_ARGS"
    if {![installtest_p]} { untested $testname; continue }
    if {![utrace_p]} { untested $testname; continue }
    send_log "Testing ${testname}\n"

    # Compile our test program.
    set res [target_compile $srcpath $exepath executable $flags]
    if { $res != "" } {
	verbose "target_compile for $exepath failed: $res" 2
	send_log "$testname: unable to compile $srcpath\n"
	untested $testname       
	return
    }

    # Run the test.
    stap_run $testname no_load $output_string -g $stppath -c $exepath

    catch {exec rm -f $exepath foobar}
}
	
# The second pass is for systems that support 32-bit executables
# (either exclusively or in addition to 64-bit executables).
set do_32_bit_pass 1    
switch -regexp $::tcl_platform(machine) {
    ^(x86_64|ppc64)$ {
	set flags "additional_flags=-m32"
    }
    ^s390x$ {
	set flags "additional_flags=-m31"
    }
    ^ia64$ {
	set do_32_bit_pass 0
    }
}

if {$do_32_bit_pass} {
    set testname "32_BIT_UTRACE_SYSCALL_ARGS"
    if {![installtest_p]} { untested $testname; continue }
    if {![utrace_p]} { untested $testname; continue }
    send_log "Testing ${testname}\n"

    # Compile our test program
    set res [target_compile $srcpath $exepath executable $flags]
    if { $res != "" } {
	verbose "target_compile for $exepath failed: $res" 2
	send_log "$testname: unable to compile $srcpath\n"
	untested $testname       
	return
    }

    stap_run $testname no_load $output_string -g $stppath -c $exepath

    catch {exec rm -f $exepath foobar}
}
