xref: /aosp_15_r20/external/bcc/examples/tracing/nodejs_http_server.py (revision 387f9dfdfa2baef462e92476d413c7bc2470293e)
1*387f9dfdSAndroid Build Coastguard Worker#!/usr/bin/python
2*387f9dfdSAndroid Build Coastguard Worker#
3*387f9dfdSAndroid Build Coastguard Worker# nodejs_http_server    Basic example of node.js USDT tracing.
4*387f9dfdSAndroid Build Coastguard Worker#                       For Linux, uses BCC, BPF. Embedded C.
5*387f9dfdSAndroid Build Coastguard Worker#
6*387f9dfdSAndroid Build Coastguard Worker# USAGE: nodejs_http_server PID
7*387f9dfdSAndroid Build Coastguard Worker#
8*387f9dfdSAndroid Build Coastguard Worker# Copyright 2016 Netflix, Inc.
9*387f9dfdSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License")
10*387f9dfdSAndroid Build Coastguard Worker
11*387f9dfdSAndroid Build Coastguard Workerfrom __future__ import print_function
12*387f9dfdSAndroid Build Coastguard Workerfrom bcc import BPF, USDT
13*387f9dfdSAndroid Build Coastguard Workerfrom bcc.utils import printb
14*387f9dfdSAndroid Build Coastguard Workerimport sys
15*387f9dfdSAndroid Build Coastguard Worker
16*387f9dfdSAndroid Build Coastguard Workerif len(sys.argv) < 2:
17*387f9dfdSAndroid Build Coastguard Worker    print("USAGE: nodejs_http_server PID")
18*387f9dfdSAndroid Build Coastguard Worker    exit()
19*387f9dfdSAndroid Build Coastguard Workerpid = sys.argv[1]
20*387f9dfdSAndroid Build Coastguard Workerdebug = 0
21*387f9dfdSAndroid Build Coastguard Worker
22*387f9dfdSAndroid Build Coastguard Worker# load BPF program
23*387f9dfdSAndroid Build Coastguard Workerbpf_text = """
24*387f9dfdSAndroid Build Coastguard Worker#include <uapi/linux/ptrace.h>
25*387f9dfdSAndroid Build Coastguard Workerint do_trace(struct pt_regs *ctx) {
26*387f9dfdSAndroid Build Coastguard Worker    uint64_t addr;
27*387f9dfdSAndroid Build Coastguard Worker    char path[128]={0};
28*387f9dfdSAndroid Build Coastguard Worker    bpf_usdt_readarg(6, ctx, &addr);
29*387f9dfdSAndroid Build Coastguard Worker    bpf_probe_read_user(&path, sizeof(path), (void *)addr);
30*387f9dfdSAndroid Build Coastguard Worker    bpf_trace_printk("path:%s\\n", path);
31*387f9dfdSAndroid Build Coastguard Worker    return 0;
32*387f9dfdSAndroid Build Coastguard Worker};
33*387f9dfdSAndroid Build Coastguard Worker"""
34*387f9dfdSAndroid Build Coastguard Worker
35*387f9dfdSAndroid Build Coastguard Worker# enable USDT probe from given PID
36*387f9dfdSAndroid Build Coastguard Workeru = USDT(pid=int(pid))
37*387f9dfdSAndroid Build Coastguard Workeru.enable_probe(probe="http__server__request", fn_name="do_trace")
38*387f9dfdSAndroid Build Coastguard Workerif debug:
39*387f9dfdSAndroid Build Coastguard Worker    print(u.get_text())
40*387f9dfdSAndroid Build Coastguard Worker    print(bpf_text)
41*387f9dfdSAndroid Build Coastguard Worker
42*387f9dfdSAndroid Build Coastguard Worker# initialize BPF
43*387f9dfdSAndroid Build Coastguard Workerb = BPF(text=bpf_text, usdt_contexts=[u])
44*387f9dfdSAndroid Build Coastguard Worker
45*387f9dfdSAndroid Build Coastguard Worker# header
46*387f9dfdSAndroid Build Coastguard Workerprint("%-18s %-16s %-6s %s" % ("TIME(s)", "COMM", "PID", "ARGS"))
47*387f9dfdSAndroid Build Coastguard Worker
48*387f9dfdSAndroid Build Coastguard Worker# format output
49*387f9dfdSAndroid Build Coastguard Workerwhile 1:
50*387f9dfdSAndroid Build Coastguard Worker    try:
51*387f9dfdSAndroid Build Coastguard Worker        (task, pid, cpu, flags, ts, msg) = b.trace_fields()
52*387f9dfdSAndroid Build Coastguard Worker    except ValueError:
53*387f9dfdSAndroid Build Coastguard Worker        print("value error")
54*387f9dfdSAndroid Build Coastguard Worker        continue
55*387f9dfdSAndroid Build Coastguard Worker    except KeyboardInterrupt:
56*387f9dfdSAndroid Build Coastguard Worker        exit()
57*387f9dfdSAndroid Build Coastguard Worker    printb(b"%-18.9f %-16s %-6d %s" % (ts, task, pid, msg))
58