-
Notifications
You must be signed in to change notification settings - Fork 201
/
stap++
executable file
·536 lines (439 loc) · 12.7 KB
/
stap++
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
#!/usr/bin/env perl
use strict;
use warnings;
use Getopt::Long qw( GetOptions :config no_ignore_case);
use File::Spec ();
use File::Temp qw( tempdir );
use FindBin ();
sub quote_sh_args ($);
GetOptions("D=s@", \(my $D_opts),
"c=s", \(my $shcmd),
"d=s@", \(my $d_opts),
"arg=s%", \(my $args),
"args", \(my $print_args),
"dump-src", \(my $dump_src),
"sample-pid=i", \(my $sample_pid),
"exec=s", \(my $exec_path),
"help", \(my $help),
"master=i", \(my $master_pid),
"x=i", \(my $pid),
"e=s", \(my $src),
"v", \(my $verbose),
"skip-badvars", \(my $skip_badvars),
"I=s@", \(my $Inc))
or die usage();
push @$Inc, '.', "$FindBin::Bin/tapset";
#warn "Inc: @$Inc\n";
if ($^O ne 'linux') {
die "Only linux is supported but I am on $^O.\n";
}
if ($print_args && $args && %$args) {
die "No --arg is allowed when --args is specified.\n";
}
my $ver = `stap -V 2>&1`;
if (!defined $ver) {
die "Systemtap not installed or its \"stap\" utility is not visible to the PATH environment: $!\n";
}
if ($ver =~ /version\s+(\d+\.\d+)/i) {
my $v = $1;
my $min_ver = 2.3;
if ($v < $min_ver) {
die "ERROR: at least systemtap $min_ver is required but found $v\n";
}
} else {
die "ERROR: unknown version of systemtap:\n$ver\n";
}
if ($help) {
print usage();
exit;
}
my %set_D_opts;
for my $opt (@$D_opts) {
#warn "$opt\n";
if ($opt =~ /^([_A-Z]+)=/) {
$set_D_opts{$1} = 1;
}
}
if (!$set_D_opts{MAXACTION}) {
push @$D_opts, "MAXACTION=100000";
}
if (!$set_D_opts{MAXMAPENTRIES}) {
push @$D_opts, "MAXMAPENTRIES=5000";
}
if (!$set_D_opts{MAXBACKTRACE}) {
push @$D_opts, "MAXBACKTRACE=200";
}
my $infile;
if (!defined $src) {
$infile = shift
or die "No input file specified.\n";
$src = read_src_file($infile);
} else {
$infile = "-e";
}
my (%StdVars, %UsedLibs, %DSOs, %LibPaths, %UsedStdVars);
my @stap_opts;
if ($verbose) {
push @stap_opts, "-v";
}
if ($d_opts) {
for my $opt (@$d_opts) {
push @stap_opts, '-d', $opt;
}
}
if (defined $skip_badvars) {
push @stap_opts, "--skip-badvars";
}
for my $opt (@$D_opts) {
push @stap_opts, "-D$opt";
}
if (defined $exec_path) {
if (!-f $exec_path) {
die "$exec_path not found.\n";
}
$StdVars{exec_path} = $exec_path;
push @stap_opts, '-d', $exec_path;
open my $in, "ldd $exec_path|"
or die "cannot run the command \"ldd $exec_path\": $!\n";
while (<$in>) {
if (m{\s+(\/\S+\.so(?:\.\d+)*)}) {
my $path = $1;
#warn "Found DSO $path.\n";
$DSOs{$path} = 1;
}
}
for my $path (sort keys %DSOs) {
push @stap_opts, '-d', $path;
}
close $in;
}
if (defined $pid) {
if (defined $master_pid) {
die "-x <pid> and --master <pid> are exclusive.\n";
}
push @stap_opts, "-x", $pid;
my $exec_file = "/proc/$pid/exe";
if (!-f $exec_file) {
die "Nginx process $pid is not running or ",
"you do not have enough permissions.\n";
}
$StdVars{pid_ok} = gen_pid_test_condition();
$StdVars{target} = $pid;
if (!defined $exec_path) {
$exec_path = readlink $exec_file;
$StdVars{exec_path} = $exec_path;
push @stap_opts, "-d", $exec_path;
}
process_dso($pid);
}
if (defined $master_pid) {
#push @stap_opts, "-x", $master_pid;
my $exec_file = "/proc/$master_pid/exe";
if (!-f $exec_file) {
die "Nginx process $master_pid is not running or ",
"you do not have enough permissions.\n";
}
if (!defined $exec_path) {
$exec_path = readlink $exec_file;
$StdVars{exec_path} = $exec_path;
push @stap_opts, "-d", $exec_path;
}
# process DSO files
my @pids = get_child_processes($master_pid);
if (@pids == 0) {
die "No child processes found for $master_pid.\n";
}
$StdVars{pid_ok} = gen_pid_test_condition(\@pids);
$StdVars{target} = "@pids";
my $pid = $pids[0];
process_dso($pid);
}
if ($sample_pid) {
process_dso($sample_pid);
}
if (!defined $StdVars{pid_ok} && defined $exec_path) {
$StdVars{pid_ok} = '1';
}
if (!defined $StdVars{target} && defined $exec_path) {
$StdVars{target} = 'ANY';
}
if (!$print_args) {
while (my ($k, $v) = each %$args) {
#warn "getting $k => $v";
$StdVars{"arg_$k"} = $v;
}
}
my %used_args;
my $stap_src = process_src($infile, $src);
if ($dump_src) {
print $stap_src;
exit;
}
if ($print_args) {
for my $name (sort keys %used_args) {
my $default = $used_args{$name};
print "\t--arg $name=VALUE";
if (defined $default) {
print " (default: $default)\n";
} else {
print "\n";
}
}
exit;
}
for my $key (keys %$args) {
if (!$UsedStdVars{"arg_$key"}) {
my $val = $args->{$key};
if (!defined $val) {
$val = '';
}
warn "WARNING: \$^arg_$key is defined by \"--arg $key=$val\", "
."but never used.\n";
}
}
my $tmpdir = tempdir("stapxx-XXXXXXXX", CLEANUP => 1);
while (my ($lib, $src) = each %UsedLibs) {
my $outfile = "$tmpdir/$lib.stp";
open my $out, ">$outfile"
or die "Cannot open $outfile for writing: $!\n";
print $out $src;
close $out;
}
push @stap_opts, "-I", $tmpdir;
if (defined $shcmd) {
push @stap_opts, "-c", $shcmd;
}
my $cmd = "stap " . quote_sh_args(\@stap_opts) . " -";
#warn $cmd;
open my $in, "|$cmd"
or die "Cannot run stap: $!\n";
print $in $stap_src;
close $in;
sub eval_usr_var {
my ($file, $usr_vars, $var) = @_;
if (defined $usr_vars->{$var}) {
return $usr_vars->{$var};
}
die "$file: line $.: Undefined user varaible \$*$var.\n";
}
sub eval_std_var {
my ($file, $var, $trait_name, $trait_val) = @_;
$UsedStdVars{$var} = 1;
if (defined $StdVars{$var}) {
return $StdVars{$var};
}
if (defined $trait_name) {
#warn "trait: $trait_name";
if ($trait_name eq 'default') {
if ($print_args && $var =~ /^arg_(\w+)$/) {
$used_args{$1} = $trait_val;
}
$StdVars{$var} = $trait_val;
return $trait_val;
} else {
die "$file: line $.: unknown trait name: $trait_name\n";
}
}
if ($print_args) {
if ($var =~ /^arg_(\w+)$/) {
$used_args{$1} = undef;
}
return '';
}
if ($var eq 'exec_path') {
die "$file: line $.: \$^exec_path is used but neither -x <pid> ",
"nor --exec <path> is specified.\n";
} elsif ($var =~ /^arg_(\w+)$/) {
die "$file: line $.: \$^$var is used but no --arg $1=VAL option is specified.\n";
} elsif ($var =~ /^(lib\w+)_path$/) {
my $prefix = $1;
my $libpath = find_dso_path($prefix);
if (!$libpath) {
warn "$file: line $.: $prefix is not found, assuming it is statically linked.\n";
if (!defined $StdVars{exec_path}) {
die "No -x <pid> option is specified.\n";
}
$LibPaths{$prefix} = $StdVars{exec_path};
return $StdVars{exec_path};
}
return $libpath;
} else {
die "$file: line $.: Undefined built-in variable \$^$var.\n";
}
}
sub find_dso_path {
my $pat = shift;
my $path = $LibPaths{$pat};
if ($path) {
return $path;
}
my $name;
if ($pat !~ /^lib(\S+)/) {
die "bad pattern: $pat";
}
$name = $1;
my $found_path;
for my $path (sort keys %DSOs) {
#warn "checking $path against $pat";
if ($path =~ m{\blib\Q$name\E[-.\d]*\.so(?:\.\d+)*$}) {
$LibPaths{$pat} = $path;
$found_path = $path;
warn "Found exact match for $pat: $path\n";
last;
}
if ($path =~ m{\b(?:lib)?\Q$name\E[^/\s]*?\.so(?:\.\d+)*$}) {
if ($found_path) {
warn "Ignored ambiguous library $path for \"$pat\"\n";
next;
}
$LibPaths{$pat} = $path;
$found_path = $path;
}
}
return $found_path;
}
sub read_src_file {
my $infile = shift;
open my $in, $infile
or die "Cannot open $infile for reading: $!\n";
my $src = do { local $/; <$in> };
close $in;
return $src;
}
sub use_libs {
my ($file, $libs) = @_;
#warn "libs: $libs";
my @libs = split /\s*,\s*/, $libs;
for my $lib (@libs) {
#warn "processing $lib...";
my $path = join("/", split /\./, $lib) . ".sxx";
#warn $path;
for my $dir (@$Inc) {
my $abspath = File::Spec->catfile($dir, $path);
#warn "Testing $abspath\n";
#warn "exist: ", (-f $abspath) ? 1 : 0;
if (-f $abspath) {
my $src = read_src_file($abspath);
$UsedLibs{$lib} = process_src($abspath, $src);
goto next_lib;
}
}
die "$file: line $.: cannot find \@use library $lib\n";
next_lib:
}
return "";
}
sub process_src {
my ($file, $src) = @_;
my %usr_vars;
my @bits;
my $libname_pat = qr/(?:\w+(?:\.\w+)*)/;
# process the input file
open my $in, '<', \$src or die $!;
while (<$in>) {
if ($. == 1 && /^\#!/) {
$_ = "#!/usr/bin/env stap\n";
next;
}
s{\$\^(arg_\w+)(?:\s*:(\w+)\s*\((.*?)\))?}{eval_std_var($file, $1, $2, $3)}eg;
s{\@pfunc\s*\(\s*(\w+)\s*\)}{process("\$^exec_path").function("$1")}g;
s{\$\^(\w+|lib[-.\w]+_path)(?:\s*:(\w+)\s*\((.*?)\))?}{eval_std_var($file, $1, $2, $3)}eg;
s{\$\*(\w+)\s*:=\s*(\&?\@(?:cast|var)\(.*?\)(?:\[\d+\])?)}{$usr_vars{$1} = $2; ""}eg;
s{\$\*(\w+)}{eval_usr_var($file, \%usr_vars, $1)}eg;
s{\@use\s+($libname_pat(?:\s*,\s*$libname_pat)*)}{use_libs($file, $1)}eg;
} continue {
push @bits, $_;
}
close $in;
return join '', @bits;
}
sub usage {
return <<'_EOC_';
Usage:
stap++ [optoins] [infile]
Options:
--arg NM=VAL Specify extra user arguments (for $^arg_NM).
--args Print all available arguments for --arg.
-c CMD start the probes, run CMD, and exit when it finishes
-d PATH Load debug info for the specified objects
-D NM=VAL Emit macro definition into generated C code.
-e SCRIPT Run given script.
--exec PATH Specify the executable file path to be traced.
--help Print this help.
-I PATH Specify the stap++ tapset library search directory.
--master PID Specify the master pid whose child processes are traced.
--sample-pid PID Sample process to inspect DSO objects to load via -d
-v Be verbose.
-x PID Sets target() to PID (also for $^exec_path and $^libxxx_path).
Examples:
stap++ -x 12345 -e 'probe begin { println("hello") exit() }'
stap++ -x 12345 infile.ss
_EOC_
}
sub get_child_processes {
my $pid = shift;
my @files = glob "/proc/[0-9]*/stat";
my @children;
for my $file (@files) {
#print "file: $file\n";
if ($file =~ m{^/proc/$pid/}) {
next;
}
open my $in, $file or next;
my $line = <$in>;
close $in;
if ($line =~ /^(\d+) \S+ \S+ (\d+)/) {
my ($child, $parent) = ($1, $2);
if ($parent eq $pid) {
push @children, $child;
}
}
}
@children = sort { $a <=> $b } @children;
return @children;
}
sub gen_pid_test_condition {
my $pids = shift;
if (!$pids) {
return "pid() == target()";
}
my @c;
for my $pid (@$pids) {
push @c, "pid() == $pid";
}
return '(' . join(" || ", @c) . ')';
}
sub process_dso {
my $pid = shift;
my $maps_file = "/proc/$pid/maps";
open my $in, $maps_file
or die "Cannot open $maps_file for reading: $!\n";
while (<$in>) {
if (m{\S+\.so(?:\.\d+)*$}) {
my $path = $&;
$DSOs{$path} = 1;
#warn "seeing $path";
}
}
for my $path (sort keys %DSOs) {
push @stap_opts, "-d", $path;
}
}
sub quote_sh_args ($) {
my ($args) = @_;
for my $arg (@$args) {
if ($arg =~ m{^[- "&%;,|?*.+=\w:/()]*$}) {
if ($arg =~ /[ "&%;,|?*()]/) {
$arg = "'$arg'";
}
next;
}
$arg =~ s/\\/\\\\/g;
$arg =~ s/'/\\'/g;
$arg =~ s/\n/\\n/g;
$arg =~ s/\r/\\r/g;
$arg =~ s/\t/\\t/g;
$arg = "\$'$arg'";
}
return "@$args";
}