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
|
#!/usr/bin/python
# @lint-avoid-python-3-compatibility-imports
#
# uflow Trace method execution flow in high-level languages.
# For Linux, uses BCC, eBPF.
#
# USAGE: uflow [-C CLASS] [-M METHOD] [-v] {java,perl,php,python,ruby,tcl} pid
#
# Copyright 2016 Sasha Goldshtein
# Licensed under the Apache License, Version 2.0 (the "License")
#
# 27-Oct-2016 Sasha Goldshtein Created this.
from __future__ import print_function
import argparse
from bcc import BPF, USDT, utils
import ctypes as ct
import time
import os
languages = ["java", "perl", "php", "python", "ruby", "tcl"]
examples = """examples:
./uflow -l java 185 # trace Java method calls in process 185
./uflow -l ruby 134 # trace Ruby method calls in process 134
./uflow -M indexOf -l java 185 # trace only 'indexOf'-prefixed methods
./uflow -C '<stdin>' -l python 180 # trace only REPL-defined methods
"""
parser = argparse.ArgumentParser(
description="Trace method execution flow in high-level languages.",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=examples)
parser.add_argument("-l", "--language", choices=languages,
help="language to trace")
parser.add_argument("pid", type=int, help="process id to attach to")
parser.add_argument("-M", "--method",
help="trace only calls to methods starting with this prefix")
parser.add_argument("-C", "--class", dest="clazz",
help="trace only calls to classes starting with this prefix")
parser.add_argument("-v", "--verbose", action="store_true",
help="verbose mode: print the BPF program (for debugging purposes)")
parser.add_argument("--ebpf", action="store_true",
help=argparse.SUPPRESS)
args = parser.parse_args()
usdt = USDT(pid=args.pid)
program = """
struct call_t {
u64 depth; // first bit is direction (0 entry, 1 return)
u64 pid; // (tgid << 32) + pid from bpf_get_current...
char clazz[80];
char method[80];
};
BPF_PERF_OUTPUT(calls);
BPF_HASH(entry, u64, u64);
"""
prefix_template = """
static inline bool prefix_%s(char *actual) {
char expected[] = "%s";
for (int i = 0; i < sizeof(expected) - 1; ++i) {
if (expected[i] != actual[i]) {
return false;
}
}
return true;
}
"""
if args.clazz:
program += prefix_template % ("class", args.clazz)
if args.method:
program += prefix_template % ("method", args.method)
trace_template = """
int NAME(struct pt_regs *ctx) {
u64 *depth, zero = 0, clazz = 0, method = 0 ;
struct call_t data = {};
READ_CLASS
READ_METHOD
bpf_probe_read_user(&data.clazz, sizeof(data.clazz), (void *)clazz);
bpf_probe_read_user(&data.method, sizeof(data.method), (void *)method);
FILTER_CLASS
FILTER_METHOD
data.pid = bpf_get_current_pid_tgid();
depth = entry.lookup_or_try_init(&data.pid, &zero);
if (!depth) {
depth = &zero;
}
data.depth = DEPTH;
UPDATE
calls.perf_submit(ctx, &data, sizeof(data));
return 0;
}
"""
def enable_probe(probe_name, func_name, read_class, read_method, is_return):
global program, trace_template, usdt
depth = "*depth + 1" if not is_return else "*depth | (1ULL << 63)"
update = "++(*depth);" if not is_return else "if (*depth) --(*depth);"
filter_class = "if (!prefix_class(data.clazz)) { return 0; }" \
if args.clazz else ""
filter_method = "if (!prefix_method(data.method)) { return 0; }" \
if args.method else ""
program += trace_template.replace("NAME", func_name) \
.replace("READ_CLASS", read_class) \
.replace("READ_METHOD", read_method) \
.replace("FILTER_CLASS", filter_class) \
.replace("FILTER_METHOD", filter_method) \
.replace("DEPTH", depth) \
.replace("UPDATE", update)
usdt.enable_probe_or_bail(probe_name, func_name)
usdt = USDT(pid=args.pid)
language = args.language
if not language:
language = utils.detect_language(languages, args.pid)
if language == "java":
enable_probe("method__entry", "java_entry",
"bpf_usdt_readarg(2, ctx, &clazz);",
"bpf_usdt_readarg(4, ctx, &method);", is_return=False)
enable_probe("method__return", "java_return",
"bpf_usdt_readarg(2, ctx, &clazz);",
"bpf_usdt_readarg(4, ctx, &method);", is_return=True)
elif language == "perl":
enable_probe("sub__entry", "perl_entry",
"bpf_usdt_readarg(2, ctx, &clazz);",
"bpf_usdt_readarg(1, ctx, &method);", is_return=False)
enable_probe("sub__return", "perl_return",
"bpf_usdt_readarg(2, ctx, &clazz);",
"bpf_usdt_readarg(1, ctx, &method);", is_return=True)
elif language == "php":
enable_probe("function__entry", "php_entry",
"bpf_usdt_readarg(4, ctx, &clazz);",
"bpf_usdt_readarg(1, ctx, &method);", is_return=False)
enable_probe("function__return", "php_return",
"bpf_usdt_readarg(4, ctx, &clazz);",
"bpf_usdt_readarg(1, ctx, &method);", is_return=True)
elif language == "python":
enable_probe("function__entry", "python_entry",
"bpf_usdt_readarg(1, ctx, &clazz);", # filename really
"bpf_usdt_readarg(2, ctx, &method);", is_return=False)
enable_probe("function__return", "python_return",
"bpf_usdt_readarg(1, ctx, &clazz);", # filename really
"bpf_usdt_readarg(2, ctx, &method);", is_return=True)
elif language == "ruby":
enable_probe("method__entry", "ruby_entry",
"bpf_usdt_readarg(1, ctx, &clazz);",
"bpf_usdt_readarg(2, ctx, &method);", is_return=False)
enable_probe("method__return", "ruby_return",
"bpf_usdt_readarg(1, ctx, &clazz);",
"bpf_usdt_readarg(2, ctx, &method);", is_return=True)
enable_probe("cmethod__entry", "ruby_centry",
"bpf_usdt_readarg(1, ctx, &clazz);",
"bpf_usdt_readarg(2, ctx, &method);", is_return=False)
enable_probe("cmethod__return", "ruby_creturn",
"bpf_usdt_readarg(1, ctx, &clazz);",
"bpf_usdt_readarg(2, ctx, &method);", is_return=True)
elif language == "tcl":
enable_probe("proc__args", "tcl_entry",
"", # no class/file info available
"bpf_usdt_readarg(1, ctx, &method);", is_return=False)
enable_probe("proc__return", "tcl_return",
"", # no class/file info available
"bpf_usdt_readarg(1, ctx, &method);", is_return=True)
else:
print("No language detected; use -l to trace a language.")
exit(1)
if args.ebpf or args.verbose:
if args.verbose:
print(usdt.get_text())
print(program)
if args.ebpf:
exit()
bpf = BPF(text=program, usdt_contexts=[usdt])
print("Tracing method calls in %s process %d... Ctrl-C to quit." %
(language, args.pid))
print("%-3s %-6s %-6s %-8s %s" % ("CPU", "PID", "TID", "TIME(us)", "METHOD"))
class CallEvent(ct.Structure):
_fields_ = [
("depth", ct.c_ulonglong),
("pid", ct.c_ulonglong),
("clazz", ct.c_char * 80),
("method", ct.c_char * 80)
]
start_ts = time.time()
def print_event(cpu, data, size):
event = ct.cast(data, ct.POINTER(CallEvent)).contents
depth = event.depth & (~(1 << 63))
direction = "<- " if event.depth & (1 << 63) else "-> "
print("%-3d %-6d %-6d %-8.3f %-40s" % (cpu, event.pid >> 32,
event.pid & 0xFFFFFFFF, time.time() - start_ts,
(" " * (depth - 1)) + direction + \
event.clazz.decode('utf-8', 'replace') + "." + \
event.method.decode('utf-8', 'replace')))
bpf["calls"].open_perf_buffer(print_event)
while 1:
try:
bpf.perf_buffer_poll()
except KeyboardInterrupt:
exit()
|