/usr/share/doc/bpfcc-tools/examples/tracing
NameSizeModeActions
biolatpcts.py33120755editdlrm
biolatpcts_example.txt6500644editdlrm
bitehist.py13970755editdlrm
bitehist_example.txt12080644editdlrm
CMakeLists.txt2760644editdlrm
dddos.py38180755editdlrm
dddos_example.txt21120644editdlrm
disksnoop.py19470755editdlrm
disksnoop_example.txt15870644editdlrm
hello_fields.py6790755editdlrm
hello_perf_output.py12700755editdlrm
hello_perf_output_using_ns.py18430755editdlrm
kvm_hypercall.py15200755editdlrm
kvm_hypercall.txt17820644editdlrm
mallocstacks.py19420755editdlrm
mysqld_query.py17010755editdlrm
mysqld_query_example.txt4990644editdlrm
nflatency.py62140755editdlrm
nodejs_http_server.py13760755editdlrm
nodejs_http_server_example.txt2760644editdlrm
stacksnoop.py32520755editdlrm
stacksnoop_example.txt28710644editdlrm
stack_buildid_example.py31060755editdlrm
strlen_count.py13310755editdlrm
strlen_hist.py18560755editdlrm
strlen_hist_ifunc.py38000755editdlrm
strlen_snoop.py13840755editdlrm
sync_timing.py13900755editdlrm
task_switch.c4990644editdlrm
task_switch.py4860755editdlrm
tcpv4connect.py24130755editdlrm
tcpv4connect_example.txt10630644editdlrm
trace_fields.py5890755editdlrm
trace_perf_output.py16000755editdlrm
undump.py36020755editdlrm
undump_example.txt8860644editdlrm
urandomread-explicit.py15110755editdlrm
urandomread.py10320755editdlrm
urandomread_example.txt6750644editdlrm
vfsreadlat.c8960644editdlrm
vfsreadlat.py13360755editdlrm
vfsreadlat_example.txt36190644editdlrm
Edit: /usr/share/doc/bpfcc-tools/examples/tracing/stack_buildid_example.py (3106B)
#!/usr/bin/python # # An example usage of stack_build_id # Most of the code here is borrowed from tools/profile.py # # Steps for using this code # 1) Start ping program in one terminal eg invocation: ping google.com -i0.001 # 2) Change the path of libc specified in b.add_module() below # 3) Invoke the script as 'python stack_buildid_example.py' # 4) o/p of the tool is as shown below # python example/tracing/stack_buildid_example.py # sendto # - ping (5232) # 2 # # REQUIRES: Linux 4.17+ (BPF_BUILD_ID support) # Licensed under the Apache License, Version 2.0 (the "License") # 03-Jan-2019 Vijay Nag from __future__ import print_function from bcc import BPF, PerfType, PerfSWConfig from sys import stderr from time import sleep import argparse import signal import os import subprocess import errno import multiprocessing import ctypes as ct def Get_libc_path(): # A small helper function that returns full path # of libc in the system cmd = 'cat /proc/self/maps | grep libc | awk \'{print $6}\' | uniq' output = subprocess.check_output(cmd, shell=True) if not isinstance(output, str): output = output.decode() return output.split('\n')[0] bpf_text = """ #include #include #include struct key_t { u32 pid; int user_stack_id; char name[TASK_COMM_LEN]; }; BPF_HASH(counts, struct key_t); BPF_STACK_TRACE_BUILDID(stack_traces, 128); int do_perf_event(struct bpf_perf_event_data *ctx) { u32 pid = bpf_get_current_pid_tgid() >> 32; // create map key struct key_t key = {.pid = pid}; bpf_get_current_comm(&key.name, sizeof(key.name)); key.user_stack_id = stack_traces.get_stackid(&ctx->regs, BPF_F_USER_STACK); if (key.user_stack_id >= 0) { counts.increment(key); } return 0; } """ b = BPF(text=bpf_text) b.attach_perf_event(ev_type=PerfType.SOFTWARE, ev_config=PerfSWConfig.CPU_CLOCK, fn_name="do_perf_event", sample_period=0, sample_freq=49, cpu=0) # Add the list of libraries/executables to the build sym cache for sym resolution # Change the libc path if it is different on a different machine. # libc.so and ping are added here so that any symbols pertaining to # libc or ping are resolved. More executables/libraries can be added here. b.add_module(Get_libc_path()) b.add_module("/usr/sbin/sshd") b.add_module("/bin/ping") counts = b.get_table("counts") stack_traces = b.get_table("stack_traces") duration = 2 def signal_handler(signal, frame): print() try: sleep(duration) except KeyboardInterrupt: # as cleanup can take some time, trap Ctrl-C: signal.signal(signal.SIGINT, signal.SIG_IGN) user_stack=[] for k,v in sorted(counts.items(), key=lambda counts: counts[1].value): user_stack = [] if k.user_stack_id < 0 else \ stack_traces.walk(k.user_stack_id) user_stack=list(user_stack) for addr in user_stack: print(" %s" % b.sym(addr, k.pid).decode('utf-8', 'replace')) print(" %-16s %s (%d)" % ("-", k.name.decode('utf-8', 'replace'), k.pid)) print(" %d\n" % v.value)