blob: 9745b3d59b3b13f64fe715dd4699d11ac435a08d [file] [log] [blame]
Alexey Ivanovcc01a9c2019-01-16 09:50:46 -08001#!/usr/bin/python
Sasha Goldshtein989057d2016-10-25 04:08:10 -07002# @lint-avoid-python-3-compatibility-imports
3#
4# uthreads Trace thread creation/destruction events in high-level languages.
5# For Linux, uses BCC, eBPF.
6#
Marko Myllynen9f3662e2018-10-10 21:48:53 +03007# USAGE: uthreads [-l {c,java,none}] [-v] pid
Sasha Goldshtein989057d2016-10-25 04:08:10 -07008#
9# Copyright 2016 Sasha Goldshtein
10# Licensed under the Apache License, Version 2.0 (the "License")
11#
12# 25-Oct-2016 Sasha Goldshtein Created this.
13
14from __future__ import print_function
15import argparse
Paul Chaignon4bb6d7f2017-03-30 19:05:40 +020016from bcc import BPF, USDT, utils
Sasha Goldshtein989057d2016-10-25 04:08:10 -070017import ctypes as ct
18import time
Paul Chaignon4bb6d7f2017-03-30 19:05:40 +020019import os
20
Marko Myllynen9f3662e2018-10-10 21:48:53 +030021languages = ["c", "java"]
Sasha Goldshtein989057d2016-10-25 04:08:10 -070022
23examples = """examples:
Marko Myllynen9f3662e2018-10-10 21:48:53 +030024 ./uthreads -l java 185 # trace Java threads in process 185
25 ./uthreads -l none 12245 # trace only pthreads in process 12245
Sasha Goldshtein989057d2016-10-25 04:08:10 -070026"""
27parser = argparse.ArgumentParser(
28 description="Trace thread creation/destruction events in " +
29 "high-level languages.",
30 formatter_class=argparse.RawDescriptionHelpFormatter,
31 epilog=examples)
Paul Chaignon4bb6d7f2017-03-30 19:05:40 +020032parser.add_argument("-l", "--language", choices=languages + ["none"],
Sasha Goldshteinbf0cf122016-10-25 04:28:06 -070033 help="language to trace (none for pthreads only)")
Sasha Goldshtein989057d2016-10-25 04:08:10 -070034parser.add_argument("pid", type=int, help="process id to attach to")
35parser.add_argument("-v", "--verbose", action="store_true",
36 help="verbose mode: print the BPF program (for debugging purposes)")
Marko Myllynen27e7aea2018-09-26 20:09:07 +030037parser.add_argument("--ebpf", action="store_true",
38 help=argparse.SUPPRESS)
Sasha Goldshtein989057d2016-10-25 04:08:10 -070039args = parser.parse_args()
40
41usdt = USDT(pid=args.pid)
42
43program = """
44struct thread_event_t {
45 u64 runtime_id;
46 u64 native_id;
47 char type[8];
48 char name[80];
49};
50
51BPF_PERF_OUTPUT(threads);
Sasha Goldshteinbf0cf122016-10-25 04:28:06 -070052
53int trace_pthread(struct pt_regs *ctx) {
54 struct thread_event_t te = {};
55 u64 start_routine = 0;
56 char type[] = "pthread";
57 te.native_id = bpf_get_current_pid_tgid() & 0xFFFFFFFF;
58 bpf_usdt_readarg(2, ctx, &start_routine);
59 te.runtime_id = start_routine; // This is really a function pointer
60 __builtin_memcpy(&te.type, type, sizeof(te.type));
61 threads.perf_submit(ctx, &te, sizeof(te));
62 return 0;
63}
Sasha Goldshtein989057d2016-10-25 04:08:10 -070064"""
Sasha Goldshteindc3a57c2017-02-08 16:02:11 -050065usdt.enable_probe_or_bail("pthread_start", "trace_pthread")
Sasha Goldshtein989057d2016-10-25 04:08:10 -070066
Paul Chaignon4bb6d7f2017-03-30 19:05:40 +020067language = args.language
68if not language:
69 language = utils.detect_language(languages, args.pid)
70
Marko Myllynen9f3662e2018-10-10 21:48:53 +030071if language == "c":
72 # Nothing to add
73 pass
74elif language == "java":
Sasha Goldshtein989057d2016-10-25 04:08:10 -070075 template = """
76int %s(struct pt_regs *ctx) {
Sasha Goldshteinbf0cf122016-10-25 04:28:06 -070077 char type[] = "%s";
Sasha Goldshtein989057d2016-10-25 04:08:10 -070078 struct thread_event_t te = {};
79 u64 nameptr = 0, id = 0, native_id = 0;
80 bpf_usdt_readarg(1, ctx, &nameptr);
81 bpf_usdt_readarg(3, ctx, &id);
82 bpf_usdt_readarg(4, ctx, &native_id);
Sumanth Korikkar023154c2020-04-20 05:54:57 -050083 bpf_probe_read_user(&te.name, sizeof(te.name), (void *)nameptr);
Sasha Goldshtein989057d2016-10-25 04:08:10 -070084 te.runtime_id = id;
85 te.native_id = native_id;
Sasha Goldshteinbf0cf122016-10-25 04:28:06 -070086 __builtin_memcpy(&te.type, type, sizeof(te.type));
Sasha Goldshtein989057d2016-10-25 04:08:10 -070087 threads.perf_submit(ctx, &te, sizeof(te));
88 return 0;
89}
90 """
91 program += template % ("trace_start", "start")
92 program += template % ("trace_stop", "stop")
Sasha Goldshteindc3a57c2017-02-08 16:02:11 -050093 usdt.enable_probe_or_bail("thread__start", "trace_start")
94 usdt.enable_probe_or_bail("thread__stop", "trace_stop")
Sasha Goldshtein989057d2016-10-25 04:08:10 -070095
Marko Myllynen27e7aea2018-09-26 20:09:07 +030096if args.ebpf or args.verbose:
97 if args.verbose:
98 print(usdt.get_text())
Sasha Goldshtein989057d2016-10-25 04:08:10 -070099 print(program)
Marko Myllynen27e7aea2018-09-26 20:09:07 +0300100 if args.ebpf:
101 exit()
Sasha Goldshtein989057d2016-10-25 04:08:10 -0700102
103bpf = BPF(text=program, usdt_contexts=[usdt])
Sasha Goldshteinbf0cf122016-10-25 04:28:06 -0700104print("Tracing thread events in process %d (language: %s)... Ctrl-C to quit." %
Paul Chaignon4bb6d7f2017-03-30 19:05:40 +0200105 (args.pid, language or "none"))
Sasha Goldshtein989057d2016-10-25 04:08:10 -0700106print("%-8s %-16s %-8s %-30s" % ("TIME", "ID", "TYPE", "DESCRIPTION"))
107
108class ThreadEvent(ct.Structure):
109 _fields_ = [
110 ("runtime_id", ct.c_ulonglong),
111 ("native_id", ct.c_ulonglong),
112 ("type", ct.c_char * 8),
113 ("name", ct.c_char * 80),
114 ]
115
116start_ts = time.time()
117
118def print_event(cpu, data, size):
119 event = ct.cast(data, ct.POINTER(ThreadEvent)).contents
Sasha Goldshteinbf0cf122016-10-25 04:28:06 -0700120 name = event.name
121 if event.type == "pthread":
Sasha Goldshteinb1bff012017-02-08 23:25:28 -0500122 name = bpf.sym(event.runtime_id, args.pid, show_module=True)
Sasha Goldshteinbf0cf122016-10-25 04:28:06 -0700123 tid = event.native_id
124 else:
125 tid = "R=%s/N=%s" % (event.runtime_id, event.native_id)
Sasha Goldshtein989057d2016-10-25 04:08:10 -0700126 print("%-8.3f %-16s %-8s %-30s" % (
Sasha Goldshteinbf0cf122016-10-25 04:28:06 -0700127 time.time() - start_ts, tid, event.type, name))
Sasha Goldshtein989057d2016-10-25 04:08:10 -0700128
129bpf["threads"].open_perf_buffer(print_event)
130while 1:
Jerome Marchand51671272018-12-19 01:57:24 +0100131 try:
132 bpf.perf_buffer_poll()
133 except KeyboardInterrupt:
134 exit()