1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
# Simple function to test that systemtap can generate instument a module
# function, install it, and get some output.
set test "kmodule"
set test2 "kprobe_module"
if {![installtest_p]} {
untested $test
return
}
set build_dir ""
set uname [exec /bin/uname -r]
proc build_and_install_module {} {
global build_dir
global srcdir subdir
# Create the build directory and populate it
if {[catch {exec mktemp -d staptestXXXXXX} build_dir]} {
verbose -log "Failed to create temporary directory: $build_dir"
return 0
}
catch {exec cp $srcdir/$subdir/stap_kmodule.c $build_dir/}
catch {exec cp -p $srcdir/$subdir/stap_kmodule.Makefile $build_dir/Makefile }
# Build the module
if {[catch {exec make -C $build_dir clean} res]} {
verbose -log "$res"
return 0
}
catch {exec make -C $build_dir} res
if {![file exists $build_dir/stap_kmodule.ko]} {
verbose -log "$res"
return 0
}
set res [as_root [list cp $build_dir/stap_kmodule.ko /lib/modules/$::uname/kernel/]]
if { $res != 0 } {
verbose -log "$res"
return 0
}
# Install the module
set res [as_root [list /sbin/insmod $build_dir/stap_kmodule.ko]]
if {$res != 0} {
verbose -log "$res"
return 0
}
return 1
}
proc cleanup_module {} {
global build_dir
as_root [list /bin/rm -f /lib/modules/$::uname/kernel/stap_kmodule.ko]
as_root [list /sbin/rmmod stap_kmodule]
if {$build_dir != ""} {
catch { exec rm -rf $build_dir }
}
}
proc kmodule_load {} {
# Trigger the test module
if {[file exists /proc/stap_kmodule_cmd]} {
catch {exec echo 0 > /proc/stap_kmodule_cmd}
return 0
} else {
return 1
}
}
proc kmodule_load2 {} {
global build_dir
# Unload the test module (in case it was leftover from a previous
# run).
as_root [list /sbin/rmmod stap_kmodule]
# Load the test module
set res [as_root [list /sbin/insmod $build_dir/stap_kmodule.ko]]
if { $res != 0 } {
verbose -log "$res"
return 1
}
# Trigger the test module
if {[file exists /proc/stap_kmodule_cmd]} {
catch {exec echo 0 > /proc/stap_kmodule_cmd}
return 0
} else {
return 1
}
# Unload the test module
set res [as_root [list /sbin/rmmod stap_kmodule]]
if { $res != 0 } {
verbose -log "$res"
return 1
}
}
set output_string "count = 1\r\n"
if {[build_and_install_module] == 0} {
verbose -log "BUILD FAILED"
fail "$test (could not build/install module)"
return
} else {
pass "$test (built and installed module)"
}
# Test 'module("foo").function("bar")
stap_run $test kmodule_load $output_string $srcdir/$subdir/$test.stp
# Test 'kprobe.module("foo").function("bar")
stap_run $test2 kmodule_load $output_string $srcdir/$subdir/$test2.stp
# Now let's test and see if the same scripts work when the test module
# is loaded *after* the systemtap script starts.
set test_suffix "(loaded after)"
if {![module_refresh_p]} {
untested "$subdir/$test.stp $test_suffix"
untested "$subdir/$test2.stp $test_suffix"
cleanup_module
return
}
as_root [list /sbin/rmmod stap_kmodule]
# Test 'module("foo").function("bar")
set test_file "$subdir/$test.stp"
stap_run "$test_file $test_suffix" kmodule_load2 \
$output_string $srcdir/$test_file
# Test 'kprobe.module("foo").function("bar")
set test_file "$subdir/$test2.stp"
stap_run "$test_file $test_suffix" kmodule_load2 \
$output_string $srcdir/$test_file
cleanup_module
|