Sasha Goldshtein | e725b14 | 2016-10-26 12:52:06 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # @lint-avoid-python-3-compatibility-imports |
| 3 | # |
| 4 | # uflow Trace method execution flow in high-level languages. |
| 5 | # For Linux, uses BCC, eBPF. |
| 6 | # |
Sasha Goldshtein | cfb5ee7 | 2017-02-08 14:32:51 -0500 | [diff] [blame] | 7 | # USAGE: uflow [-C CLASS] [-M METHOD] [-v] {java,python,ruby,php} pid |
Sasha Goldshtein | e725b14 | 2016-10-26 12:52:06 -0700 | [diff] [blame] | 8 | # |
| 9 | # Copyright 2016 Sasha Goldshtein |
| 10 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 11 | # |
| 12 | # 27-Oct-2016 Sasha Goldshtein Created this. |
| 13 | |
| 14 | from __future__ import print_function |
| 15 | import argparse |
Paul Chaignon | 4bb6d7f | 2017-03-30 19:05:40 +0200 | [diff] [blame] | 16 | from bcc import BPF, USDT, utils |
Sasha Goldshtein | e725b14 | 2016-10-26 12:52:06 -0700 | [diff] [blame] | 17 | import ctypes as ct |
| 18 | import time |
Paul Chaignon | 4bb6d7f | 2017-03-30 19:05:40 +0200 | [diff] [blame] | 19 | import os |
| 20 | |
| 21 | languages = ["java", "python", "ruby", "php"] |
Sasha Goldshtein | e725b14 | 2016-10-26 12:52:06 -0700 | [diff] [blame] | 22 | |
| 23 | examples = """examples: |
Paul Chaignon | 4bb6d7f | 2017-03-30 19:05:40 +0200 | [diff] [blame] | 24 | ./uflow -l java 185 # trace Java method calls in process 185 |
| 25 | ./uflow -l ruby 134 # trace Ruby method calls in process 134 |
| 26 | ./uflow -M indexOf -l java 185 # trace only 'indexOf'-prefixed methods |
| 27 | ./uflow -C '<stdin>' -l python 180 # trace only REPL-defined methods |
Sasha Goldshtein | e725b14 | 2016-10-26 12:52:06 -0700 | [diff] [blame] | 28 | """ |
| 29 | parser = argparse.ArgumentParser( |
| 30 | description="Trace method execution flow in high-level languages.", |
| 31 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 32 | epilog=examples) |
Paul Chaignon | 4bb6d7f | 2017-03-30 19:05:40 +0200 | [diff] [blame] | 33 | parser.add_argument("-l", "--language", choices=languages, |
Sasha Goldshtein | e725b14 | 2016-10-26 12:52:06 -0700 | [diff] [blame] | 34 | help="language to trace") |
| 35 | parser.add_argument("pid", type=int, help="process id to attach to") |
| 36 | parser.add_argument("-M", "--method", |
| 37 | help="trace only calls to methods starting with this prefix") |
| 38 | parser.add_argument("-C", "--class", dest="clazz", |
| 39 | help="trace only calls to classes starting with this prefix") |
| 40 | parser.add_argument("-v", "--verbose", action="store_true", |
| 41 | help="verbose mode: print the BPF program (for debugging purposes)") |
| 42 | args = parser.parse_args() |
| 43 | |
| 44 | usdt = USDT(pid=args.pid) |
| 45 | |
| 46 | program = """ |
| 47 | struct call_t { |
| 48 | u64 depth; // first bit is direction (0 entry, 1 return) |
| 49 | u64 pid; // (tgid << 32) + pid from bpf_get_current... |
| 50 | u64 timestamp; // ns |
| 51 | char clazz[80]; |
| 52 | char method[80]; |
| 53 | }; |
| 54 | |
| 55 | BPF_PERF_OUTPUT(calls); |
| 56 | BPF_HASH(entry, u64, u64); |
| 57 | """ |
| 58 | |
| 59 | prefix_template = """ |
| 60 | static inline bool prefix_%s(char *actual) { |
| 61 | char expected[] = "%s"; |
| 62 | for (int i = 0; i < sizeof(expected) - 1; ++i) { |
| 63 | if (expected[i] != actual[i]) { |
| 64 | return false; |
| 65 | } |
| 66 | } |
| 67 | return true; |
| 68 | } |
| 69 | """ |
| 70 | |
| 71 | if args.clazz: |
| 72 | program += prefix_template % ("class", args.clazz) |
| 73 | if args.method: |
| 74 | program += prefix_template % ("method", args.method) |
| 75 | |
| 76 | trace_template = """ |
| 77 | int NAME(struct pt_regs *ctx) { |
| 78 | u64 *depth, zero = 0, clazz = 0, method = 0 ; |
| 79 | struct call_t data = {}; |
| 80 | |
| 81 | READ_CLASS |
| 82 | READ_METHOD |
| 83 | bpf_probe_read(&data.clazz, sizeof(data.clazz), (void *)clazz); |
| 84 | bpf_probe_read(&data.method, sizeof(data.method), (void *)method); |
| 85 | |
| 86 | FILTER_CLASS |
| 87 | FILTER_METHOD |
| 88 | |
| 89 | data.pid = bpf_get_current_pid_tgid(); |
| 90 | data.timestamp = bpf_ktime_get_ns(); |
| 91 | depth = entry.lookup_or_init(&data.pid, &zero); |
| 92 | data.depth = DEPTH; |
| 93 | UPDATE |
| 94 | |
| 95 | calls.perf_submit(ctx, &data, sizeof(data)); |
| 96 | return 0; |
| 97 | } |
| 98 | """ |
| 99 | |
| 100 | def enable_probe(probe_name, func_name, read_class, read_method, is_return): |
| 101 | global program, trace_template, usdt |
| 102 | depth = "*depth + 1" if not is_return else "*depth | (1ULL << 63)" |
Paul Chaignon | 956ca1c | 2017-03-04 20:07:56 +0100 | [diff] [blame] | 103 | update = "++(*depth);" if not is_return else "if (*depth) --(*depth);" |
Sasha Goldshtein | e725b14 | 2016-10-26 12:52:06 -0700 | [diff] [blame] | 104 | filter_class = "if (!prefix_class(data.clazz)) { return 0; }" \ |
| 105 | if args.clazz else "" |
| 106 | filter_method = "if (!prefix_method(data.method)) { return 0; }" \ |
| 107 | if args.method else "" |
| 108 | program += trace_template.replace("NAME", func_name) \ |
| 109 | .replace("READ_CLASS", read_class) \ |
| 110 | .replace("READ_METHOD", read_method) \ |
| 111 | .replace("FILTER_CLASS", filter_class) \ |
| 112 | .replace("FILTER_METHOD", filter_method) \ |
| 113 | .replace("DEPTH", depth) \ |
| 114 | .replace("UPDATE", update) |
Sasha Goldshtein | dc3a57c | 2017-02-08 16:02:11 -0500 | [diff] [blame] | 115 | usdt.enable_probe_or_bail(probe_name, func_name) |
Sasha Goldshtein | e725b14 | 2016-10-26 12:52:06 -0700 | [diff] [blame] | 116 | |
| 117 | usdt = USDT(pid=args.pid) |
| 118 | |
Paul Chaignon | 4bb6d7f | 2017-03-30 19:05:40 +0200 | [diff] [blame] | 119 | language = args.language |
| 120 | if not language: |
| 121 | language = utils.detect_language(languages, args.pid) |
| 122 | |
| 123 | if language == "java": |
Sasha Goldshtein | e725b14 | 2016-10-26 12:52:06 -0700 | [diff] [blame] | 124 | enable_probe("method__entry", "java_entry", |
| 125 | "bpf_usdt_readarg(2, ctx, &clazz);", |
| 126 | "bpf_usdt_readarg(4, ctx, &method);", is_return=False) |
| 127 | enable_probe("method__return", "java_return", |
| 128 | "bpf_usdt_readarg(2, ctx, &clazz);", |
| 129 | "bpf_usdt_readarg(4, ctx, &method);", is_return=True) |
Paul Chaignon | 4bb6d7f | 2017-03-30 19:05:40 +0200 | [diff] [blame] | 130 | elif language == "python": |
Sasha Goldshtein | e725b14 | 2016-10-26 12:52:06 -0700 | [diff] [blame] | 131 | enable_probe("function__entry", "python_entry", |
| 132 | "bpf_usdt_readarg(1, ctx, &clazz);", # filename really |
| 133 | "bpf_usdt_readarg(2, ctx, &method);", is_return=False) |
| 134 | enable_probe("function__return", "python_return", |
| 135 | "bpf_usdt_readarg(1, ctx, &clazz);", # filename really |
| 136 | "bpf_usdt_readarg(2, ctx, &method);", is_return=True) |
Paul Chaignon | 4bb6d7f | 2017-03-30 19:05:40 +0200 | [diff] [blame] | 137 | elif language == "ruby": |
Sasha Goldshtein | e725b14 | 2016-10-26 12:52:06 -0700 | [diff] [blame] | 138 | enable_probe("method__entry", "ruby_entry", |
| 139 | "bpf_usdt_readarg(1, ctx, &clazz);", |
| 140 | "bpf_usdt_readarg(2, ctx, &method);", is_return=False) |
| 141 | enable_probe("method__return", "ruby_return", |
| 142 | "bpf_usdt_readarg(1, ctx, &clazz);", |
| 143 | "bpf_usdt_readarg(2, ctx, &method);", is_return=True) |
| 144 | enable_probe("cmethod__entry", "ruby_centry", |
| 145 | "bpf_usdt_readarg(1, ctx, &clazz);", |
| 146 | "bpf_usdt_readarg(2, ctx, &method);", is_return=False) |
| 147 | enable_probe("cmethod__return", "ruby_creturn", |
| 148 | "bpf_usdt_readarg(1, ctx, &clazz);", |
| 149 | "bpf_usdt_readarg(2, ctx, &method);", is_return=True) |
Paul Chaignon | 4bb6d7f | 2017-03-30 19:05:40 +0200 | [diff] [blame] | 150 | elif language == "php": |
Sasha Goldshtein | cfb5ee7 | 2017-02-08 14:32:51 -0500 | [diff] [blame] | 151 | enable_probe("function__entry", "php_entry", |
| 152 | "bpf_usdt_readarg(4, ctx, &clazz);", |
| 153 | "bpf_usdt_readarg(1, ctx, &method);", is_return=False) |
| 154 | enable_probe("function__return", "php_return", |
| 155 | "bpf_usdt_readarg(4, ctx, &clazz);", |
| 156 | "bpf_usdt_readarg(1, ctx, &method);", is_return=True) |
Paul Chaignon | 4bb6d7f | 2017-03-30 19:05:40 +0200 | [diff] [blame] | 157 | else: |
| 158 | print("No language detected; use -l to trace a language.") |
| 159 | exit(1) |
Sasha Goldshtein | e725b14 | 2016-10-26 12:52:06 -0700 | [diff] [blame] | 160 | |
| 161 | if args.verbose: |
| 162 | print(usdt.get_text()) |
| 163 | print(program) |
| 164 | |
| 165 | bpf = BPF(text=program, usdt_contexts=[usdt]) |
| 166 | print("Tracing method calls in %s process %d... Ctrl-C to quit." % |
Paul Chaignon | 4bb6d7f | 2017-03-30 19:05:40 +0200 | [diff] [blame] | 167 | (language, args.pid)) |
Sasha Goldshtein | e725b14 | 2016-10-26 12:52:06 -0700 | [diff] [blame] | 168 | print("%-3s %-6s %-6s %-8s %s" % ("CPU", "PID", "TID", "TIME(us)", "METHOD")) |
| 169 | |
| 170 | class CallEvent(ct.Structure): |
| 171 | _fields_ = [ |
| 172 | ("depth", ct.c_ulonglong), |
| 173 | ("pid", ct.c_ulonglong), |
| 174 | ("timestamp", ct.c_ulonglong), |
| 175 | ("clazz", ct.c_char * 80), |
| 176 | ("method", ct.c_char * 80) |
| 177 | ] |
| 178 | |
| 179 | start_ts = time.time() |
| 180 | |
| 181 | def print_event(cpu, data, size): |
| 182 | event = ct.cast(data, ct.POINTER(CallEvent)).contents |
| 183 | depth = event.depth & (~(1 << 63)) |
| 184 | direction = "<- " if event.depth & (1 << 63) else "-> " |
| 185 | print("%-3d %-6d %-6d %-8.3f %-40s" % (cpu, event.pid >> 32, |
| 186 | event.pid & 0xFFFFFFFF, time.time() - start_ts, |
| 187 | (" " * (depth - 1)) + direction + event.clazz + "." + event.method)) |
| 188 | |
| 189 | bpf["calls"].open_perf_buffer(print_event) |
| 190 | while 1: |
| 191 | bpf.kprobe_poll() |