xref: /aosp_15_r20/external/bcc/tools/threadsnoop.py (revision 387f9dfdfa2baef462e92476d413c7bc2470293e)
1*387f9dfdSAndroid Build Coastguard Worker#!/usr/bin/env python
2*387f9dfdSAndroid Build Coastguard Worker# @lint-avoid-python-3-compatibility-imports
3*387f9dfdSAndroid Build Coastguard Worker#
4*387f9dfdSAndroid Build Coastguard Worker# threadsnoop   List new thread creation.
5*387f9dfdSAndroid Build Coastguard Worker#               For Linux, uses BCC, eBPF. Embedded C.
6*387f9dfdSAndroid Build Coastguard Worker#
7*387f9dfdSAndroid Build Coastguard Worker# Copyright (c) 2019 Brendan Gregg.
8*387f9dfdSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License").
9*387f9dfdSAndroid Build Coastguard Worker# This was originally created for the BPF Performance Tools book
10*387f9dfdSAndroid Build Coastguard Worker# published by Addison Wesley. ISBN-13: 9780136554820
11*387f9dfdSAndroid Build Coastguard Worker# When copying or porting, include this comment.
12*387f9dfdSAndroid Build Coastguard Worker#
13*387f9dfdSAndroid Build Coastguard Worker# 02-Jul-2019   Brendan Gregg   Ported from bpftrace to BCC.
14*387f9dfdSAndroid Build Coastguard Worker
15*387f9dfdSAndroid Build Coastguard Workerfrom __future__ import print_function
16*387f9dfdSAndroid Build Coastguard Workerfrom bcc import BPF
17*387f9dfdSAndroid Build Coastguard Worker
18*387f9dfdSAndroid Build Coastguard Worker# load BPF program
19*387f9dfdSAndroid Build Coastguard Workerb = BPF(text="""
20*387f9dfdSAndroid Build Coastguard Worker#include <linux/sched.h>
21*387f9dfdSAndroid Build Coastguard Worker
22*387f9dfdSAndroid Build Coastguard Workerstruct data_t {
23*387f9dfdSAndroid Build Coastguard Worker    u64 ts;
24*387f9dfdSAndroid Build Coastguard Worker    u32 pid;
25*387f9dfdSAndroid Build Coastguard Worker    u64 start;
26*387f9dfdSAndroid Build Coastguard Worker    char comm[TASK_COMM_LEN];
27*387f9dfdSAndroid Build Coastguard Worker};
28*387f9dfdSAndroid Build Coastguard Worker
29*387f9dfdSAndroid Build Coastguard WorkerBPF_PERF_OUTPUT(events);
30*387f9dfdSAndroid Build Coastguard Worker
31*387f9dfdSAndroid Build Coastguard Workervoid do_entry(struct pt_regs *ctx) {
32*387f9dfdSAndroid Build Coastguard Worker    struct data_t data = {};
33*387f9dfdSAndroid Build Coastguard Worker    data.ts = bpf_ktime_get_ns();
34*387f9dfdSAndroid Build Coastguard Worker    data.pid = bpf_get_current_pid_tgid() >> 32;
35*387f9dfdSAndroid Build Coastguard Worker    data.start = PT_REGS_PARM3(ctx);
36*387f9dfdSAndroid Build Coastguard Worker    bpf_get_current_comm(&data.comm, sizeof(data.comm));
37*387f9dfdSAndroid Build Coastguard Worker
38*387f9dfdSAndroid Build Coastguard Worker    events.perf_submit(ctx, &data, sizeof(data));
39*387f9dfdSAndroid Build Coastguard Worker};
40*387f9dfdSAndroid Build Coastguard Worker""")
41*387f9dfdSAndroid Build Coastguard Worker
42*387f9dfdSAndroid Build Coastguard Worker# Since version 2.34, pthread features are integrated in libc
43*387f9dfdSAndroid Build Coastguard Workertry:
44*387f9dfdSAndroid Build Coastguard Worker    b.attach_uprobe(name="pthread", sym="pthread_create", fn_name="do_entry")
45*387f9dfdSAndroid Build Coastguard Workerexcept Exception:
46*387f9dfdSAndroid Build Coastguard Worker    b.attach_uprobe(name="c", sym="pthread_create", fn_name="do_entry")
47*387f9dfdSAndroid Build Coastguard Worker
48*387f9dfdSAndroid Build Coastguard Workerprint("%-10s %-7s %-16s %s" % ("TIME(ms)", "PID", "COMM", "FUNC"))
49*387f9dfdSAndroid Build Coastguard Worker
50*387f9dfdSAndroid Build Coastguard Workerstart_ts = 0
51*387f9dfdSAndroid Build Coastguard Worker
52*387f9dfdSAndroid Build Coastguard Worker# process event
53*387f9dfdSAndroid Build Coastguard Workerdef print_event(cpu, data, size):
54*387f9dfdSAndroid Build Coastguard Worker    global start_ts
55*387f9dfdSAndroid Build Coastguard Worker    event = b["events"].event(data)
56*387f9dfdSAndroid Build Coastguard Worker    if start_ts == 0:
57*387f9dfdSAndroid Build Coastguard Worker        start_ts = event.ts
58*387f9dfdSAndroid Build Coastguard Worker    func = b.sym(event.start, event.pid)
59*387f9dfdSAndroid Build Coastguard Worker    if (func == "[unknown]"):
60*387f9dfdSAndroid Build Coastguard Worker        func = hex(event.start)
61*387f9dfdSAndroid Build Coastguard Worker    print("%-10d %-7d %-16s %s" % ((event.ts - start_ts) / 1000000,
62*387f9dfdSAndroid Build Coastguard Worker        event.pid, event.comm, func))
63*387f9dfdSAndroid Build Coastguard Worker
64*387f9dfdSAndroid Build Coastguard Workerb["events"].open_perf_buffer(print_event)
65*387f9dfdSAndroid Build Coastguard Workerwhile 1:
66*387f9dfdSAndroid Build Coastguard Worker    try:
67*387f9dfdSAndroid Build Coastguard Worker        b.perf_buffer_poll()
68*387f9dfdSAndroid Build Coastguard Worker    except KeyboardInterrupt:
69*387f9dfdSAndroid Build Coastguard Worker        exit()
70