• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
11from __future__ import print_function
12from bcc import BPF, USDT
13import sys
14
15if len(sys.argv) < 2:
16    print("USAGE: nodejs_http_server PID")
17    exit()
18pid = sys.argv[1]
19debug = 0
20
21# load BPF program
22bpf_text = """
23#include <uapi/linux/ptrace.h>
24int do_trace(struct pt_regs *ctx) {
25    uint64_t addr;
26    char path[128]={0};
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
35u = USDT(pid=int(pid))
36u.enable_probe(probe="http__server__request", fn_name="do_trace")
37if debug:
38    print(u.get_text())
39    print(bpf_text)
40
41# initialize BPF
42b = BPF(text=bpf_text, usdt_contexts=[u])
43
44# header
45print("%-18s %-16s %-6s %s" % ("TIME(s)", "COMM", "PID", "ARGS"))
46
47# format output
48while 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))
55