-
Notifications
You must be signed in to change notification settings - Fork 7
/
nhttpsnoop
executable file
·534 lines (441 loc) · 12.4 KB
/
nhttpsnoop
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
#!/bin/bash
#
# nhttpsnoop: snoop Node HTTP requests
#
function usage
{
cat >&2 << USAGE
usage: $nhs_arg0 [-cglns] [-t argtype] [-o col,...] [-p pid,...]
Trace Node.js HTTP client, server, and garbage collection activity. By
default, all requests for all Node.js HTTP servers on the system are traced,
and information about each one is displayed as it completes. You can also
specify:
-c Trace HTTP client activity (request/response pairs).
See "Notes" below.
-g Trace garbage collections.
-s Trace HTTP server activity (request/response pairs).
With none of these options, "-s" is assumed. For examples:
# $nhs_arg0 # trace HTTP server activity only
# $nhs_arg0 -c # trace HTTP client activity only
# $nhs_arg0 -cs # trace HTTP client and server activity
# $nhs_arg0 -g # trace garbage collections only
# $nhs_arg0 -cgs # trace HTTP client and server activity and GCs
Other options:
-l Display two lines, one each for the beginning and end of each
HTTP request. For server requests, these correspond with
receiving the request and sending the response. For client
requests, these correspond with sending the request and
receiving the response.
This is useful when you want to see how operations are
actually interleaved instead of just how long each one takes.
-n Don't actually run DTrace, but instead just print out the D
script that would be used.
-o col,... Display the specified columns instead of the default output.
Available columns include:
latency time in microseconds between the request being
received and the response being sent.
method Request's HTTP method
path Request's HTTP URL path
(excludes query parameters)
pid process identifier
probe indicates the type of event
("client", "server", or "gc")
raddr Remote host's IPv4 address
rport Remote host's TCP port
time relative time of the event from when $nhs_arg0
started
url Request's full HTTP URL, including query parameters
which Indicates with an arrow whether this is an
incoming or outgoing request.
Some fields may not apply to all events.
-p pid,... Only trace the specified processes.
-t ARGTYPE Specify which probe arguments to use, which must be one of
"translated" or "simple". Translated arguments are more
extensible, more efficient, and the only reliable approach on
systems that support them. Untranslated arguments are
required on OS X, which doesn't support USDT translators.
The default value is selected based on your system and you
should never need to override this.
Notes
This tool uses the Node.js DTrace provider and dtrace(1M). You must have
appropriate permissions to run it.
Data for HTTP client requests is not reliable when multiple requests are
issued concurrently for the same remote server. Unfortunately, there's no
way to reliably associate request and response pairs in this case. As a
result, some records may be missing from the output, and others may have
incorrect time and latency information.
USAGE
exit 2
}
function fail
{
echo "$nhs_arg0: $*" >&2
exit 1
}
#
# optsplit str
#
# Split a comma- or whitespace-separated string into its constituent pieces.
# This is used to split the -p option (typically something like "-p pid1 pid2")
# and the "-o" option (typically something like "-o col1,col2").
#
function optsplit
{
for arg in "$@"; do
IFS=,
for carg in $arg; do
echo $carg
done
IFS=
done
}
#
# join sep arg1 ...
#
# Join the arguments (strings) with the given separator string.
#
function join
{
local sep=$1
shift
echo -n "$1"
shift
for elt in "$@"; do
echo -ne "$sep"
echo -n "$elt"
done
}
#
# emit_printf fmt arg1 ...
#
# Emit a DTrace "printf" statement.
#
function emit_printf
{
local fmt="$1"
shift
echo -ne "\tprintf($fmt,\n\t "
join ",\n\t " "$@"
echo ");"
}
#
# emit_header
#
# Emit a header row for the currently selected columns.
#
function emit_header
{
local fmts= args=
for column in $nhs_cols; do
case $column in
fd)
fmts="$fmts %4s"
args="$args \"FD\""
;;
latency)
fmts="$fmts %10s"
args="$args \"LATENCY\""
;;
method)
fmts="$fmts %-6s"
args="$args \"METHOD\""
;;
path)
fmts="$fmts %-20s"
args="$args \"PATH\""
;;
pid)
fmts="$fmts %6s"
args="$args \"PID\""
;;
probe)
fmts="$fmts %-6s"
args="$args \"PROBE\""
;;
raddr)
fmts="$fmts %-16s"
args="$args \"RADDR\""
;;
rport)
fmts="$fmts %5s"
args="$args \"RPORT\""
;;
time)
fmts="$fmts %-12s"
args="$args \"TIME\""
;;
url)
fmts="$fmts %-20s"
args="$args \"URL\""
;;
which)
fmts="$fmts %2s"
args="$args \"\""
;;
esac
done
emit_printf "\"$(join " " $fmts)\\\\n\"" $args
}
#
# emit_row request|response conn method uri
#
# Emit the body of a DTrace clause that will print out the fields requested by
# the user. Because the http-server-request and http-server-response probes
# access their arguments differently, the caller specifies how to access the
# connection arguments, the method, and the URI in the current context.
#
function emit_row
{
local which=$1 fd=$2 raddr=$3 rport=$4 method=$5 uri=$6
local fmts=
local args=
local lat=
for column in $nhs_cols; do
case $column in
fd)
if [[ $which != "gc" ]]; then
fmts="$fmts %4d"
args="$args $fd"
else
fmts="$fmts %4s"
args="$args \"-\""
fi
;;
latency)
if [[ $which = "request" ]]; then
fmts="$fmts %10s"
args="$args \"-\""
else
if [[ $which = "response" ]]; then
lat="(timestamp-"
lat="${lat}rqstarts[pid,$raddr,$rport])"
else
lat="(timestamp-self->gc_start)"
fi
fmts="$fmts %4d.%-03dms"
args="$args $lat/MICROSEC"
args="$args ($lat%MICROSEC)/1000"
fi
;;
method)
fmts="$fmts %-6s"
args="$args $method"
;;
path)
fmts="$fmts %-20s"
args="$args strtok($uri,\"?\")"
;;
pid)
fmts="$fmts %6d"
args="$args pid"
;;
probe)
fmts="$fmts %-6s"
if [[ $which != "gc" ]]; then
args="$args strtok(probename+sizeof("
args="${args}\"http-\")-1,\"-\")"
else
args="$args \"gc\""
fi
;;
raddr)
fmts="$fmts %-16s"
args="$args $raddr"
;;
rport)
fmts="$fmts %5d"
args="$args $rport"
;;
time)
fmts="$fmts [%3d.%06d]"
args="$args (timestamp-start)/NANOSEC"
args="$args (timestamp-start)%NANOSEC/1000"
;;
url)
fmts="$fmts %-20s"
args="$args $uri"
;;
which)
if [[ $which = "request" ]]; then
fmts="$fmts ->"
else
fmts="$fmts <-"
fi
;;
*)
fail "invalid column name: $column"
;;
esac
done
emit_printf "\"$(join " " $fmts)\\\\n\"" $args
}
nhs_arg0=$(basename $0) # program name (for error messages)
nhs_tmpfile=/var/tmp/$nhs_arg0.$$.d # dynamically-generated D script
nhs_pids= # selected pids (-p)
nhs_cols= # selected columns (-o)
nhs_dryrun="false" # just print script and exit (-n)
nhs_tracestart="false" # print "request" events (-l)
nhs_argtype="translated" # use translated args (see below)
nhs_doclient="false" # trace client activity
nhs_dogc="false" # trace GC activity
nhs_doserver= # trace server activity
#
# OS X does not support translated arguments with USDT, so we default to using
# the untranslated versions. We don't do this on other systems (e.g., illumos,
# BSD) because the untranslated arguments are not present in older versions of
# the provider there and because the untranslated arguments are more limited if
# we ever extend the translated ones. (The untranslated versions are always
# available on OS X because the provider wasn't supported there before they were
# added.) This behavior can be overridden using the "-t" option.
#
[[ $(uname -s) = "Darwin" ]] && nhs_argtype="simple"
while getopts "cglno:p:t:s" c; do
case $c in
c) nhs_doclient="true" ;;
g) nhs_dogc="true" ;;
l) nhs_tracestart="true" ;;
n) nhs_dryrun="true" ;;
o) nhs_cols=$(optsplit $OPTARG) ;;
p) nhs_pids=$(optsplit $OPTARG) ;;
s) nhs_doserver="true" ;;
t) nhs_argtype="$OPTARG" ;;
*) usage
esac
done
if [[ $nhs_doclient = "true" || $nhs_dogc = "true" ]]; then
[[ -z "$nhs_doserver" ]] && nhs_doserver="false"
else
nhs_doserver="true"
fi
nhs_rqprobes=
nhs_rsprobes=
if [[ "$nhs_doserver" = "true" ]]; then
nhs_rqprobes="$nhs_rqprobes node*:::http-server-request"
nhs_rsprobes="$nhs_rsprobes node*:::http-server-response"
fi
if [[ "$nhs_doclient" = "true" ]]; then
nhs_rqprobes="$nhs_rqprobes node*:::http-client-request"
nhs_rsprobes="$nhs_rsprobes node*:::http-client-response"
fi
# Configure default columns.
if [[ -z "$nhs_cols" ]]; then
if [[ $nhs_tracestart = "true" ]]; then
nhs_cols="time pid probe which latency method path"
else
nhs_cols="time pid probe latency method path"
fi
fi
#
# For readability, we define bash variables to contain the D expressions used
# for the various pieces of information we need. The expression depends on the
# nhs_argtype (-t) option as well as which probe we're looking at ("request" or
# "response").
#
if [[ $nhs_argtype = "translated" ]]; then
rqarg_fd="args[1]->fd"
rqarg_raddr="args[1]->remoteAddress"
rqarg_rport="args[1]->remotePort"
rqarg_method="args[0]->method"
rqarg_url="args[0]->url"
rsarg_fd="args[0]->fd"
rsarg_raddr="args[0]->remoteAddress"
rsarg_rport="args[0]->remotePort"
elif [[ $nhs_argtype = "simple" ]]; then
rqarg_fd="-1"
rqarg_raddr="copyinstr(arg2)"
rqarg_rport="arg3"
rqarg_method="copyinstr(arg4)"
rqarg_url="copyinstr(arg5)"
rsarg_fd="-1"
rsarg_raddr="copyinstr(arg1)"
rsarg_rport="arg2"
else
echo "$nhs_arg0: invalid value for \"-t\" option: \"$nhs_argtype\""
usage
fi
cat > $nhs_tmpfile <<EOF
#!/usr/sbin/dtrace -Cs
#pragma D option quiet
#define MILLISEC (1000)
#define MICROSEC (1000 * 1000)
#define NANOSEC (1000 * 1000 * 1000)
BEGIN
{
$(emit_header)
start = timestamp;
}
EOF
if [[ $nhs_doserver = "true" || $nhs_doclient = "true" ]]; then
join ",\n" $nhs_rqprobes >> $nhs_tmpfile
echo >> $nhs_tmpfile
#
# We generate the DTrace script dynamically based on the options given.
# If -p was specified, we predicate our first probe on the given set of
# pids.
#
if [[ -n "$nhs_pids" ]]; then
set -- $nhs_pids
if [[ $# -eq 1 ]]; then
echo "/pid == $1/" >> $nhs_tmpfile
else
echo "/pid == $1" >> $nhs_tmpfile
shift
for pid in "$@"; do
echo " || pid == $pid" >> $nhs_tmpfile
done
echo "/" >> $nhs_tmpfile
fi
fi
echo "{" >> $nhs_tmpfile
[[ $nhs_tracestart = "true" ]] &&
emit_row "request" \
$rqarg_fd \
$rqarg_raddr \
$rqarg_rport \
$rqarg_method \
$rqarg_url >> $nhs_tmpfile
cat >> $nhs_tmpfile <<EOF
rqstarts[pid, $rqarg_raddr, $rqarg_rport] = timestamp;
rqmethods[pid, $rqarg_raddr, $rqarg_rport] = $rqarg_method;
rqurls[pid, $rqarg_raddr, $rqarg_rport] = $rqarg_url;
}
$(join ",\n" $nhs_rsprobes)
/rqstarts[pid, $rsarg_raddr, $rsarg_rport]/
{
$(emit_row "response" \
$rsarg_fd \
$rsarg_raddr \
$rsarg_rport \
"rqmethods[pid,$rsarg_raddr,$rsarg_rport]" \
"rqurls[pid,$rsarg_raddr,$rsarg_rport]")
rqstarts[pid, $rsarg_raddr, $rsarg_rport] = 0;
rqmethods[pid, $rsarg_raddr, $rsarg_rport] = 0;
rqurls[pid, $rsarg_raddr, $rsarg_rport] = 0;
}
EOF
fi
if [[ $nhs_dogc = "true" ]]; then
cat >> $nhs_tmpfile <<EOF
node*:::gc-start
{
self->gc_start = timestamp;
}
node*:::gc-done
/self->gc_start/
{
$(emit_row "gc" -1 '"-"' 0 '"-"' '"-"')
self->gc_start = 0;
}
EOF
fi
if [[ $nhs_dryrun = "true" ]]; then
cat $nhs_tmpfile
rm -f $nhs_tmpfile
exit 0
fi
chmod +x $nhs_tmpfile
dtrace $DTRACE_OPTS -Z -Cs $nhs_tmpfile || \
echo "$nhs_arg0: failed to invoke dtrace(1M)" >&2
rv=$?
rm -f $nhs_tmpfile
exit $rv