1*387f9dfdSAndroid Build Coastguard Worker#!/usr/bin/python 2*387f9dfdSAndroid Build Coastguard Worker# 3*387f9dfdSAndroid Build Coastguard Worker# This is a Hello World example that uses BPF_PERF_OUTPUT. 4*387f9dfdSAndroid Build Coastguard Worker 5*387f9dfdSAndroid Build Coastguard Workerfrom bcc import BPF 6*387f9dfdSAndroid Build Coastguard Workerfrom bcc.utils import printb 7*387f9dfdSAndroid Build Coastguard Worker 8*387f9dfdSAndroid Build Coastguard Worker# define BPF program 9*387f9dfdSAndroid Build Coastguard Workerprog = """ 10*387f9dfdSAndroid Build Coastguard Worker#include <linux/sched.h> 11*387f9dfdSAndroid Build Coastguard Worker 12*387f9dfdSAndroid Build Coastguard Worker// define output data structure in C 13*387f9dfdSAndroid Build Coastguard Workerstruct data_t { 14*387f9dfdSAndroid Build Coastguard Worker u32 pid; 15*387f9dfdSAndroid Build Coastguard Worker u64 ts; 16*387f9dfdSAndroid Build Coastguard Worker char comm[TASK_COMM_LEN]; 17*387f9dfdSAndroid Build Coastguard Worker}; 18*387f9dfdSAndroid Build Coastguard WorkerBPF_PERF_OUTPUT(events); 19*387f9dfdSAndroid Build Coastguard Worker 20*387f9dfdSAndroid Build Coastguard Workerint hello(struct pt_regs *ctx) { 21*387f9dfdSAndroid Build Coastguard Worker struct data_t data = {}; 22*387f9dfdSAndroid Build Coastguard Worker 23*387f9dfdSAndroid Build Coastguard Worker data.pid = bpf_get_current_pid_tgid(); 24*387f9dfdSAndroid Build Coastguard Worker data.ts = bpf_ktime_get_ns(); 25*387f9dfdSAndroid Build Coastguard Worker bpf_get_current_comm(&data.comm, sizeof(data.comm)); 26*387f9dfdSAndroid Build Coastguard Worker 27*387f9dfdSAndroid Build Coastguard Worker events.perf_submit(ctx, &data, sizeof(data)); 28*387f9dfdSAndroid Build Coastguard Worker 29*387f9dfdSAndroid Build Coastguard Worker return 0; 30*387f9dfdSAndroid Build Coastguard Worker} 31*387f9dfdSAndroid Build Coastguard Worker""" 32*387f9dfdSAndroid Build Coastguard Worker 33*387f9dfdSAndroid Build Coastguard Worker# load BPF program 34*387f9dfdSAndroid Build Coastguard Workerb = BPF(text=prog) 35*387f9dfdSAndroid Build Coastguard Workerb.attach_kprobe(event=b.get_syscall_fnname("clone"), fn_name="hello") 36*387f9dfdSAndroid Build Coastguard Worker 37*387f9dfdSAndroid Build Coastguard Worker# header 38*387f9dfdSAndroid Build Coastguard Workerprint("%-18s %-16s %-6s %s" % ("TIME(s)", "COMM", "PID", "MESSAGE")) 39*387f9dfdSAndroid Build Coastguard Worker 40*387f9dfdSAndroid Build Coastguard Worker# process event 41*387f9dfdSAndroid Build Coastguard Workerstart = 0 42*387f9dfdSAndroid Build Coastguard Workerdef print_event(cpu, data, size): 43*387f9dfdSAndroid Build Coastguard Worker global start 44*387f9dfdSAndroid Build Coastguard Worker event = b["events"].event(data) 45*387f9dfdSAndroid Build Coastguard Worker if start == 0: 46*387f9dfdSAndroid Build Coastguard Worker start = event.ts 47*387f9dfdSAndroid Build Coastguard Worker time_s = (float(event.ts - start)) / 1000000000 48*387f9dfdSAndroid Build Coastguard Worker printb(b"%-18.9f %-16s %-6d %s" % (time_s, event.comm, event.pid, 49*387f9dfdSAndroid Build Coastguard Worker b"Hello, perf_output!")) 50*387f9dfdSAndroid Build Coastguard Worker 51*387f9dfdSAndroid Build Coastguard Worker# loop with callback to print_event 52*387f9dfdSAndroid Build Coastguard Workerb["events"].open_perf_buffer(print_event) 53*387f9dfdSAndroid Build Coastguard Workerwhile 1: 54*387f9dfdSAndroid Build Coastguard Worker try: 55*387f9dfdSAndroid Build Coastguard Worker b.perf_buffer_poll() 56*387f9dfdSAndroid Build Coastguard Worker except KeyboardInterrupt: 57*387f9dfdSAndroid Build Coastguard Worker exit() 58