-
Notifications
You must be signed in to change notification settings - Fork 354
/
ngx-body-filters
executable file
·154 lines (116 loc) · 3.16 KB
/
ngx-body-filters
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
#!/usr/bin/env perl
# Copyright (C) Yichun Zhang (agentzh)
use 5.006001;
use strict;
use warnings;
use Getopt::Std qw( getopts );
my %opts;
getopts("a:dhp:n:", \%opts)
or die usage();
if ($opts{h}) {
print usage();
exit;
}
my $pid = $opts{p}
or die "No nginx process pid specified by the -p option\n";
if ($pid !~ /^\d+$/) {
die "Bad -p option value \"$pid\": not look like a pid\n";
}
my $stap_args = $opts{a} || '';
if ($^O ne 'linux') {
die "Only linux is supported but I am on $^O.\n";
}
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";
}
my $nginx_path = readlink $exec_file;
my $ver = `stap --version 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;
if ($v < 2.1) {
die "ERROR: at least systemtap 2.1 is required but found $v\n";
}
} else {
die "ERROR: unknown version of systemtap:\n$ver\n";
}
my $stap_src;
my $zone = $opts{n};
my $preamble = <<_EOC_;
probe begin {
printf("Tracing %d ($nginx_path)...\\n\\n", target())
}
_EOC_
chop $preamble;
$stap_src = <<_EOC_;
$preamble
probe process("$nginx_path").function("ngx_http_write_filter")
{
if (pid() == target()) {
#println("=============================")
begin = local_clock_us()
res = ""
top_filter = usymname(\@var("ngx_http_top_body_filter"))
#printf("top filter: %s\\n", top_filter)
b = ubacktrace();
token = tokenize(b, " ");
if (token != "") {
found = 0
for ( ;; ) {
token = tokenize("", " ");
if (token == "") {
break;
}
addr = strtol(token, 16)
sym = usymname(addr)
#printf("found sym: %s (%p)\\n", sym, addr)
if (sym == "ngx_http_header_filter") {
res = ""
break
}
res = sym . "\\n" . res
if (sym == top_filter) {
#println("found top filter!")
found = 1
break
}
}
if (!found) {
res = ""
}
}
if (res != "") {
print(res)
println("ngx_http_write_filter")
elapsed = local_clock_us() - begin
printf("\\n%d microseconds elapsed in the probe handler.\\n", elapsed)
exit()
}
}
}
_EOC_
if ($opts{d}) {
print $stap_src;
exit;
}
open my $in, "|stap --skip-badvars $stap_args -x $pid -"
or die "Cannot run stap: $!\n";
print $in $stap_src;
close $in;
sub usage {
return <<'_EOC_';
Usage:
ngx-body-filters [optoins]
Options:
-a <args> Pass extra arguments to the stap utility.
-d Dump out the systemtap script source.
-h Print this usage.
-p <pid> Specify the nginx worker process pid.
Examples:
ngx-body-filters -p 12345
_EOC_
}