forked from soarpenguin/systemtap-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stpscript2ko.pl
executable file
·192 lines (161 loc) · 4.45 KB
/
stpscript2ko.pl
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/perl
#
# use for to convert script to kernel module.
# stap -r 2.6.35.14-106.fc14.i686 nettop.stp -m nettop -p4
#
# -p NUM Stop after pass NUM.
# The passes are numbered 1-5: parse, elaborate, translate,
# compile, run. See the PROCESSING section for details.
use strict;
use warnings;
use Getopt::Long;
use File::Basename;
use Term::ANSIColor;
my $DEBUG = 0;
if ($DEBUG) {
eval q{
use Smart::Comments;
};
die $@ if $@;
}
my $uname;
chomp($uname = `uname -r`); ## $uname
my $myscript; # systemstp script to convert.
my $mymodule; # kernel module name.
my $guru; # guru mode
my $kerneldir; # kernel source code dir.
my $bufsize; # bufsize (megebyte) for kernel-to-user data transfer.
my $program = &_my_program();
my $command = 'stap';
my $usage = "
Usage: $program [option] ...
-g
Guru mode. Enable parsing of unsafe expert-level
constructs like embedded C.(Not enable default,
if script contains embedded C, must enable it.)
-h, --help
Display this help and exit
-m [modulename], --module [modulename]
The output module name(default: scriptname.ko).
-s scriptname, --script scriptname
A script must be specified.
The script name want to converted to *.ko.
-r /DIR
Build for kernel in given build tree. Can also
be set with the SYSTEMTAP_RELEASE environment variable.
-r RELEASE (default)
Build for kernel in build tree /lib/modules/RELEASE/build.
Can also be set with the SYSTEMTAP_RELEASE environment variable.
-V Display version information.
-sNUM
Use NUM megabyte buffers for kernel-to-user data transfer.
On a multiprocessor in bulk mode, this is a per-processor amount.
";
my $ret = GetOptions(
'script|s=s' => \$myscript,
'module|m=s' => \$mymodule,
'help|h' => \&usage,
'V' => \&usage,
'g' => \$guru,
'r=s' => \$kerneldir,
'size=i' => \$bufsize
);
$| =1;
if(! $ret) {
&usage();
}
unless($myscript) {
&mydie("A script must be specified.");
}
if(! -e $myscript) {
&mydie("The input script: $myscript not existed.");
}
unless($mymodule) {
if($myscript =~ /^((\w)+)\.(\w)+/) {
$mymodule = "$1";
} else {
$mymodule = "$myscript";
}
if($mymodule =~ /[^_0-9a-zA-Z]/) {
$mymodule =~ s/[^_0-9a-zA-Z]//g;
}
}
## $mymodule
## the command to finish convertion
if(! $kerneldir) {
print("\nUse current running kernel: \"$uname\"!\n");
if($uname =~ /^\'([^']+)\'/) {
$uname = $1;
}
# print "$uname\n";
} else {
if(-e $kerneldir) {
## $kerneldir
if($kerneldir =~ /((.)+)\/$/) {
$uname = $1;
} else {
$uname = $kerneldir;
}
## $uname
print("Use kernel build tree: \"$uname\"!\n");
} else {
&myprint("The kernel build tree: \"$kerneldir\" not exists!\n");
print("Use current running kernel: \"$uname\"!\n");
if($uname =~ /^\'([^']+)\'/) {
$uname = $1;
}
}
}
if($guru) {
$command = 'stap -g';
}
if($bufsize) {
$bufsize = "-s$bufsize";
} else {
$bufsize = '';
}
my $result = `$command $bufsize -r $uname $myscript -m $mymodule -p4 2>&1`;
chomp($result);
## $result
if($? != 0) {
if($result =~ 'embedded code' and !$guru) {
&myprint("The script contains embedded C. Make try in enable guru mode use -g.");
print("Trying enable guru mode.....\n");
# make try enable guru mode default.
$command = 'stap -g';
$result = `$command $bufsize -r $uname $myscript -m $mymodule -p4 2>&1`;
}
if ($result =~ 'no probes') {
&myprint("Make sure have probes.");
} elsif ($? != 0) {
&myprint("$result");
}
}
if($? == 0) {
print color("green");
print "The $myscript successful converted to $result\n";
print color("reset");
} else {
&myprint("The $myscript convertion is failed!\n");
}
##----------------------------------------------------
#
sub _my_program {
require File::Basename;
return File::Basename::basename( $0 );
}
sub usage {
print $usage;
exit;
}
sub mydie {
print color("red");
print("@_ \n");
print color("reset");
&usage();
}
sub myprint {
print color("red");
print("@_ \n");
print color("reset");
}