Debian Codegraph @

doc/SystemTap_Beginners_Guide/testsuite/systemtap.base/kmodule.exp

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