xref: /aosp_15_r20/external/bcc/examples/tracing/trace_fields.py (revision 387f9dfdfa2baef462e92476d413c7bc2470293e)
1*387f9dfdSAndroid Build Coastguard Worker#!/usr/bin/python
2*387f9dfdSAndroid Build Coastguard Worker# Copyright (c) PLUMgrid, Inc.
3*387f9dfdSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License")
4*387f9dfdSAndroid Build Coastguard Worker
5*387f9dfdSAndroid Build Coastguard Worker# This is an example of tracing an event and printing custom fields.
6*387f9dfdSAndroid Build Coastguard Worker# run in project examples directory with:
7*387f9dfdSAndroid Build Coastguard Worker# sudo ./trace_fields.py"
8*387f9dfdSAndroid Build Coastguard Worker
9*387f9dfdSAndroid Build Coastguard Workerfrom __future__ import print_function
10*387f9dfdSAndroid Build Coastguard Workerfrom bcc import BPF
11*387f9dfdSAndroid Build Coastguard Worker
12*387f9dfdSAndroid Build Coastguard Workerprog = """
13*387f9dfdSAndroid Build Coastguard Workerint hello(void *ctx) {
14*387f9dfdSAndroid Build Coastguard Worker  bpf_trace_printk("Hello, World!\\n");
15*387f9dfdSAndroid Build Coastguard Worker  return 0;
16*387f9dfdSAndroid Build Coastguard Worker}
17*387f9dfdSAndroid Build Coastguard Worker"""
18*387f9dfdSAndroid Build Coastguard Workerb = BPF(text=prog)
19*387f9dfdSAndroid Build Coastguard Workerb.attach_kprobe(event=b.get_syscall_fnname("clone"), fn_name="hello")
20*387f9dfdSAndroid Build Coastguard Workerprint("PID MESSAGE")
21*387f9dfdSAndroid Build Coastguard Workertry:
22*387f9dfdSAndroid Build Coastguard Worker    b.trace_print(fmt="{1} {5}")
23*387f9dfdSAndroid Build Coastguard Workerexcept KeyboardInterrupt:
24*387f9dfdSAndroid Build Coastguard Worker    exit()
25