Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752222AbbERFcO (ORCPT ); Mon, 18 May 2015 01:32:14 -0400 Received: from szxga02-in.huawei.com ([119.145.14.65]:34882 "EHLO szxga02-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751966AbbERFbj (ORCPT ); Mon, 18 May 2015 01:31:39 -0400 From: He Kuang To: , , , , , , , , , , CC: , , , Subject: [RFC PATCH 3/5] bpf: Add helper function for fetching variables at probe point Date: Mon, 18 May 2015 05:30:45 +0000 Message-ID: <1431927047-35144-4-git-send-email-hekuang@huawei.com> X-Mailer: git-send-email 1.8.3.4 In-Reply-To: <1431927047-35144-1-git-send-email-hekuang@huawei.com> References: <1431927047-35144-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: 3110 Lines: 106 This helper function uses kernel structure trace_probe and related fetch functions for fetching variables described in 'SEC' to bpf stack. Signed-off-by: He Kuang --- include/uapi/linux/bpf.h | 1 + kernel/trace/bpf_trace.c | 38 ++++++++++++++++++++++++++++++++++++++ samples/bpf/bpf_helpers.h | 2 ++ 3 files changed, 41 insertions(+) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index a9ebdf5..b1a7685 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -210,6 +210,7 @@ enum bpf_func_id { * Return: 0 on success */ BPF_FUNC_l4_csum_replace, + BPF_FUNC_fetch_args, __BPF_FUNC_MAX_ID, }; diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 2d56ce5..ba601da 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -12,6 +12,7 @@ #include #include #include "trace.h" +#include "trace_probe.h" static DEFINE_PER_CPU(int, bpf_prog_active); @@ -159,6 +160,39 @@ static const struct bpf_func_proto bpf_trace_printk_proto = { .arg2_type = ARG_CONST_STACK_SIZE, }; +/* Store the value of each argument */ +static void +bpf_store_trace_args(struct pt_regs *regs, struct trace_probe *tp, + u8 *data) +{ + int i; + + for (i = 0; i < tp->nr_args; i++) { + /* Just fetching data normally */ + call_fetch(&tp->args[i].fetch, regs, + data + tp->args[i].offset); + } +} + +static u64 bpf_fetch_args(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) +{ + struct pt_regs *regs = (struct pt_regs *)(long)r1; + struct trace_probe *tp = ((struct bpf_pt_regs *)regs)->tp; + void *data = (void *)(long)r2; + + bpf_store_trace_args(regs, tp, data); + + return 0; +} + +static const struct bpf_func_proto bpf_fetch_args_proto = { + .func = bpf_fetch_args, + .gpl_only = true, + .ret_type = RET_INTEGER, + .arg1_type = ARG_PTR_TO_CTX, + .arg2_type = ARG_PTR_TO_STACK, +}; + static const struct bpf_func_proto *kprobe_prog_func_proto(enum bpf_func_id func_id) { switch (func_id) { @@ -181,6 +215,10 @@ static const struct bpf_func_proto *kprobe_prog_func_proto(enum bpf_func_id func trace_printk_init_buffers(); return &bpf_trace_printk_proto; + + case BPF_FUNC_fetch_args: + return &bpf_fetch_args_proto; + default: return NULL; } diff --git a/samples/bpf/bpf_helpers.h b/samples/bpf/bpf_helpers.h index f960b5f..578a8e3 100644 --- a/samples/bpf/bpf_helpers.h +++ b/samples/bpf/bpf_helpers.h @@ -21,6 +21,8 @@ static unsigned long long (*bpf_ktime_get_ns)(void) = (void *) BPF_FUNC_ktime_get_ns; static int (*bpf_trace_printk)(const char *fmt, int fmt_size, ...) = (void *) BPF_FUNC_trace_printk; +static int (*bpf_fetch_args)(void *ctx, void *data) = + (void *) BPF_FUNC_fetch_args; /* llvm builtin functions that eBPF C program may use to * emit BPF_LD_ABS and BPF_LD_IND instructions -- 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/