Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751174AbbEXI26 (ORCPT ); Sun, 24 May 2015 04:28:58 -0400 Received: from szxga02-in.huawei.com ([119.145.14.65]:41883 "EHLO szxga02-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751095AbbEXI2w (ORCPT ); Sun, 24 May 2015 04:28:52 -0400 From: He Kuang To: , , , , , , , , , , , CC: , Subject: [RFC PATCH v2 14/15] samples/bpf: Add sample for testing bpf fetch args Date: Sun, 24 May 2015 08:28:10 +0000 Message-ID: <1432456091-73384-15-git-send-email-hekuang@huawei.com> X-Mailer: git-send-email 1.8.3.4 In-Reply-To: <1432456091-73384-1-git-send-email-hekuang@huawei.com> References: <1432456091-73384-1-git-send-email-hekuang@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.107.197.210] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 4144 Lines: 120 Sample code for testing bpf fetch args. Works as following steps: $ perf bpf record --object sample_bpf_fetch_args.o -- dd if=/dev/zero of=/mnt/data/test bs=4k count=3 show result in ringbuffer: $ perf script dd 1176 [000] 87706.975390: perf_bpf_probe:generic_perform_write: (ffffffff811307ea) a_ops=0xffffffff81a200e0 bytes=0x1000 page=0xffff88007c61fd80 pos=0 dd 1176 [000] 87706.975390: perf_bpf_probe:generic_perform_write: (ffffffff811307ea) a_ops=0xffffffff81a200e0 bytes=0x1000 page=0xffffea0001c44800 pos=4096 dd 1176 [000] 87706.975390: perf_bpf_probe:generic_perform_write: (ffffffff811307ea) a_ops=0xffffffff81a200e0 bytes=0x1000 page=0xffffea0001ed24c0 pos=8192 dd 1176 [000] 87706.975390: perf_bpf_probe:submit_bio: (ffffffff81310cdf) count=0x3000 rw=1 bio=0xffff88007c237a80 show result in bpf prog: $ cat /sys/kernel/debug/tracing/trace |grep dd dd-1176 [000] d... 87778.531511: : generic_perform_write(1): a_ops=ffffffff81a200e0, bytes=4096 dd-1176 [000] d... 87778.531550: : generic_perform_write(2): page =ffff88007c61fd80, pos =0 dd-1176 [000] d... 87778.532142: : generic_perform_write(1): a_ops=ffffffff81a200e0, bytes=4096 dd-1176 [000] d... 87778.532154: : generic_perform_write(2): page =ffffea0001c44800, pos =4096 dd-1176 [000] d... 87778.532342: : generic_perform_write(1): a_ops=ffffffff81a200e0, bytes=4096 dd-1176 [000] d... 87778.532354: : generic_perform_write(2): page =ffffea0001ed24c0, pos =8192 dd-1176 [000] d.h. 87778.533049: : submit_bio count=12288, rw=1, bio=ffff88007c237a80 Signed-off-by: He Kuang --- samples/bpf/Makefile | 1 + samples/bpf/sample_bpf_fetch_args.c | 62 +++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 samples/bpf/sample_bpf_fetch_args.c diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile index 8fdbd73..dc0b0e8 100644 --- a/samples/bpf/Makefile +++ b/samples/bpf/Makefile @@ -30,6 +30,7 @@ always += tracex2_kern.o always += tracex3_kern.o always += tracex4_kern.o always += tcbpf1_kern.o +always += sample_bpf_fetch_args.o HOSTCFLAGS += -I$(objtree)/usr/include diff --git a/samples/bpf/sample_bpf_fetch_args.c b/samples/bpf/sample_bpf_fetch_args.c new file mode 100644 index 0000000..5d2c80c --- /dev/null +++ b/samples/bpf/sample_bpf_fetch_args.c @@ -0,0 +1,62 @@ +/* + Sample code for bpf_fetch_args(). +*/ + +#include +#include + +#include +#include +#include "bpf_helpers.h" + +/* + * Prototype: + * ssize_t generic_perform_write(struct file *file, + * struct iov_iter *i, loff_t pos) + * Local variables: + * const struct address_space_operations *a_ops; + * unsigned long bytes; + * struct page *page; + * loff_t pos; + */ +SEC("generic_perform_write=generic_perform_write+122 file->f_mapping->a_ops bytes page pos") +int NODE_generic_perform_write(struct pt_regs *ctx, + void *a_ops, + void *bytes, + void *page, + void *pos) +{ + /* Too many args for bpf_trace_printk, show in 2 lines */ + char fmt1[] = "generic_perform_write(1): a_ops=%p, bytes=%lu\n"; + char fmt2[] = "generic_perform_write(2): page =%p, pos =%lu\n"; + + bpf_trace_printk(fmt1, sizeof(fmt1), + a_ops, (unsigned long)bytes); + bpf_trace_printk(fmt2, sizeof(fmt2), + page, (unsigned long)pos); + + return 1; +} + +/* + * Prototype: + * void submit_bio(int rw, struct bio *bio) + * Local variables: + * unsigned int count; + */ +SEC("submit_bio=submit_bio+63 count rw bio") +int NODE_submit_bio(struct pt_regs *ctx, + void *count, + void *rw, + void *bio) +{ + char fmt[] = "submit_bio count=%u, rw=%d, bio=%p\n"; + + bpf_trace_printk(fmt, sizeof(fmt), + (unsigned int)count, (int)rw, bio); + + return 1; +} + +char _license[] SEC("license") = "GPL"; +u32 _version SEC("version") = LINUX_VERSION_CODE; -- 1.8.5.2 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/