xref: /aosp_15_r20/external/bcc/examples/tracing/hello_fields.py (revision 387f9dfdfa2baef462e92476d413c7bc2470293e)
1 #!/usr/bin/python
2 #
3 # This is a Hello World example that formats output as fields.
4 
5 from bcc import BPF
6 from bcc.utils import printb
7 
8 # define BPF program
9 prog = """
10 int hello(void *ctx) {
11     bpf_trace_printk("Hello, World!\\n");
12     return 0;
13 }
14 """
15 
16 # load BPF program
17 b = BPF(text=prog)
18 b.attach_kprobe(event=b.get_syscall_fnname("clone"), fn_name="hello")
19 
20 # header
21 print("%-18s %-16s %-6s %s" % ("TIME(s)", "COMM", "PID", "MESSAGE"))
22 
23 # format output
24 while 1:
25     try:
26         (task, pid, cpu, flags, ts, msg) = b.trace_fields()
27     except ValueError:
28         continue
29     except KeyboardInterrupt:
30         exit()
31     printb(b"%-18.9f %-16s %-6d %s" % (ts, task, pid, msg))
32