Brendan Gregg | 239e863 | 2016-07-25 15:02:32 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # This is a Hello World example that formats output as fields. |
| 4 | |
| 5 | from bcc import BPF |
| 6 | |
| 7 | # define BPF program |
| 8 | prog = """ |
| 9 | int hello(void *ctx) { |
| 10 | bpf_trace_printk("Hello, World!\\n"); |
| 11 | return 0; |
| 12 | } |
| 13 | """ |
| 14 | |
| 15 | # load BPF program |
| 16 | b = BPF(text=prog) |
Yonghong Song | 83b49ad | 2018-04-24 10:15:24 -0700 | [diff] [blame] | 17 | b.attach_kprobe(event=b.get_syscall_fnname("clone"), fn_name="hello") |
Brendan Gregg | 239e863 | 2016-07-25 15:02:32 -0700 | [diff] [blame] | 18 | |
| 19 | # header |
| 20 | print("%-18s %-16s %-6s %s" % ("TIME(s)", "COMM", "PID", "MESSAGE")) |
| 21 | |
| 22 | # format output |
| 23 | while 1: |
| 24 | try: |
| 25 | (task, pid, cpu, flags, ts, msg) = b.trace_fields() |
| 26 | except ValueError: |
| 27 | continue |
| 28 | print("%-18.9f %-16s %-6d %s" % (ts, task, pid, msg)) |