blob: e32a26ea68a468e755ca55ac79991d67ea6b69ca [file] [log] [blame]
Alexey Ivanovcc01a9c2019-01-16 09:50:46 -08001#!/usr/bin/python
Brendan Gregg4f88a942016-07-22 17:11:51 -07002#
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
Gary Linbb65bea2019-02-27 16:52:35 +080013from bcc.utils import printb
Brendan Gregg4f88a942016-07-22 17:11:51 -070014import sys
15
16if len(sys.argv) < 2:
17 print("USAGE: nodejs_http_server PID")
18 exit()
19pid = sys.argv[1]
20debug = 0
21
22# load BPF program
23bpf_text = """
24#include <uapi/linux/ptrace.h>
25int do_trace(struct pt_regs *ctx) {
26 uint64_t addr;
affansyedce1ce3f2016-12-09 09:04:52 +050027 char path[128]={0};
Brendan Gregg4f88a942016-07-22 17:11:51 -070028 bpf_usdt_readarg(6, ctx, &addr);
Sumanth Korikkar023154c2020-04-20 05:54:57 -050029 bpf_probe_read_user(&path, sizeof(path), (void *)addr);
Brendan Gregg4f88a942016-07-22 17:11:51 -070030 bpf_trace_printk("path:%s\\n", path);
31 return 0;
32};
33"""
34
35# enable USDT probe from given PID
36u = USDT(pid=int(pid))
37u.enable_probe(probe="http__server__request", fn_name="do_trace")
38if debug:
39 print(u.get_text())
40 print(bpf_text)
41
42# initialize BPF
Sasha Goldshteina2370ab2016-10-06 17:52:09 -070043b = BPF(text=bpf_text, usdt_contexts=[u])
Brendan Gregg4f88a942016-07-22 17:11:51 -070044
45# header
46print("%-18s %-16s %-6s %s" % ("TIME(s)", "COMM", "PID", "ARGS"))
47
48# format output
49while 1:
50 try:
51 (task, pid, cpu, flags, ts, msg) = b.trace_fields()
52 except ValueError:
53 print("value error")
54 continue
Gary Lin593339d2019-02-27 16:54:44 +080055 except KeyboardInterrupt:
56 exit()
Gary Linbb65bea2019-02-27 16:52:35 +080057 printb(b"%-18.9f %-16s %-6d %s" % (ts, task, pid, msg))