Brendan Gregg | 4f88a94 | 2016-07-22 17:11:51 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # nodejs_http_server Basic example of node.js USDT tracing. |
| 4 | # For Linux, uses BCC, BPF. Embedded C. |
| 5 | # |
| 6 | # USAGE: nodejs_http_server PID |
| 7 | # |
| 8 | # Copyright 2016 Netflix, Inc. |
| 9 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 10 | |
| 11 | from __future__ import print_function |
| 12 | from bcc import BPF, USDT |
| 13 | import sys |
| 14 | |
| 15 | if len(sys.argv) < 2: |
| 16 | print("USAGE: nodejs_http_server PID") |
| 17 | exit() |
| 18 | pid = sys.argv[1] |
| 19 | debug = 0 |
| 20 | |
| 21 | # load BPF program |
| 22 | bpf_text = """ |
| 23 | #include <uapi/linux/ptrace.h> |
| 24 | int do_trace(struct pt_regs *ctx) { |
| 25 | uint64_t addr; |
affansyed | ce1ce3f | 2016-12-09 09:04:52 +0500 | [diff] [blame] | 26 | char path[128]={0}; |
Brendan Gregg | 4f88a94 | 2016-07-22 17:11:51 -0700 | [diff] [blame] | 27 | bpf_usdt_readarg(6, ctx, &addr); |
| 28 | bpf_probe_read(&path, sizeof(path), (void *)addr); |
| 29 | bpf_trace_printk("path:%s\\n", path); |
| 30 | return 0; |
| 31 | }; |
| 32 | """ |
| 33 | |
| 34 | # enable USDT probe from given PID |
| 35 | u = USDT(pid=int(pid)) |
| 36 | u.enable_probe(probe="http__server__request", fn_name="do_trace") |
| 37 | if debug: |
| 38 | print(u.get_text()) |
| 39 | print(bpf_text) |
| 40 | |
| 41 | # initialize BPF |
Sasha Goldshtein | a2370ab | 2016-10-06 17:52:09 -0700 | [diff] [blame] | 42 | b = BPF(text=bpf_text, usdt_contexts=[u]) |
Brendan Gregg | 4f88a94 | 2016-07-22 17:11:51 -0700 | [diff] [blame] | 43 | |
| 44 | # header |
| 45 | print("%-18s %-16s %-6s %s" % ("TIME(s)", "COMM", "PID", "ARGS")) |
| 46 | |
| 47 | # format output |
| 48 | while 1: |
| 49 | try: |
| 50 | (task, pid, cpu, flags, ts, msg) = b.trace_fields() |
| 51 | except ValueError: |
| 52 | print("value error") |
| 53 | continue |
| 54 | print("%-18.9f %-16s %-6d %s" % (ts, task, pid, msg)) |