From patchwork Tue Dec 6 07:13:54 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wang Nan X-Patchwork-Id: 86688 Delivered-To: patch@linaro.org Received: by 10.140.20.101 with SMTP id 92csp1880544qgi; Mon, 5 Dec 2016 23:16:03 -0800 (PST) X-Received: by 10.99.137.66 with SMTP id v63mr109495321pgd.117.1481008563806; Mon, 05 Dec 2016 23:16:03 -0800 (PST) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 73si18346302pfw.55.2016.12.05.23.16.03; Mon, 05 Dec 2016 23:16:03 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752875AbcLFHPr (ORCPT + 25 others); Tue, 6 Dec 2016 02:15:47 -0500 Received: from szxga02-in.huawei.com ([119.145.14.65]:46184 "EHLO szxga02-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752462AbcLFHOp (ORCPT ); Tue, 6 Dec 2016 02:14:45 -0500 Received: from 172.24.1.47 (EHLO szxeml431-hub.china.huawei.com) ([172.24.1.47]) by szxrg02-dlp.huawei.com (MOS 4.3.7-GA FastPath queued) with ESMTP id DRT24877; Tue, 06 Dec 2016 15:14:24 +0800 (CST) Received: from linux-4hy3.site (10.107.193.248) by szxeml431-hub.china.huawei.com (10.82.67.208) with Microsoft SMTP Server id 14.3.235.1; Tue, 6 Dec 2016 15:14:15 +0800 From: Wang Nan To: CC: , , Wang Nan , Arnaldo Carvalho de Melo , "Alexei Starovoitov" , He Kuang , Jiri Olsa , Zefan Li , Subject: [PATCH v4 16/18] perf clang: Include helpers to BPF scripts Date: Tue, 6 Dec 2016 07:13:54 +0000 Message-ID: <20161206071356.5312-17-wangnan0@huawei.com> X-Mailer: git-send-email 2.10.1 In-Reply-To: <20161206071356.5312-1-wangnan0@huawei.com> References: <20161206071356.5312-1-wangnan0@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.107.193.248] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Automatically include some commonly used macros and struct definitions into BPF scripts. Script writers are no longer required to define 'SEC' and 'struct bpf_map_def' in each of their scripts. Signed-off-by: Wang Nan Cc: Arnaldo Carvalho de Melo Cc: Alexei Starovoitov Cc: He Kuang Cc: Jiri Olsa Cc: Zefan Li Cc: pi3orama@163.com --- tools/perf/tests/bpf-script-example.c | 2 +- tools/perf/tests/bpf-script-test-kbuild.c | 2 ++ tools/perf/tests/bpf-script-test-prologue.c | 4 +++- tools/perf/tests/bpf-script-test-relocation.c | 3 +-- tools/perf/util/c++/Build | 1 + tools/perf/util/c++/bpf-helper-str.c | 15 +++++++++++++++ tools/perf/util/c++/clang-bpf-includes.h | 1 + tools/perf/util/c++/clang.cpp | 1 + 8 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 tools/perf/util/c++/bpf-helper-str.c -- 2.10.1 diff --git a/tools/perf/tests/bpf-script-example.c b/tools/perf/tests/bpf-script-example.c index 42dc341..e60bebf 100644 --- a/tools/perf/tests/bpf-script-example.c +++ b/tools/perf/tests/bpf-script-example.c @@ -9,6 +9,7 @@ #define BPF_ANY 0 #define BPF_MAP_TYPE_ARRAY 2 +#ifndef BUILTIN_CLANG_DEFAULT_INCLUDE struct bpf_map_def { unsigned int type; unsigned int key_size; @@ -18,7 +19,6 @@ struct bpf_map_def { #define SEC(NAME) __attribute__((section(NAME), used)) -#ifndef BUILTIN_CLANG_DEFAULT_INCLUDE #define BPF_FUNC_map_lookup_elem 1 #define BPF_FUNC_map_update_elem 2 diff --git a/tools/perf/tests/bpf-script-test-kbuild.c b/tools/perf/tests/bpf-script-test-kbuild.c index 3626924..f1b48a4 100644 --- a/tools/perf/tests/bpf-script-test-kbuild.c +++ b/tools/perf/tests/bpf-script-test-kbuild.c @@ -6,7 +6,9 @@ # error Need LINUX_VERSION_CODE # error Example: for 4.2 kernel, put 'clang-opt="-DLINUX_VERSION_CODE=0x40200" into llvm section of ~/.perfconfig' #endif +#ifndef BUILTIN_CLANG_DEFAULT_INCLUDE #define SEC(NAME) __attribute__((section(NAME), used)) +#endif #include #include diff --git a/tools/perf/tests/bpf-script-test-prologue.c b/tools/perf/tests/bpf-script-test-prologue.c index ada812b..e2176c9 100644 --- a/tools/perf/tests/bpf-script-test-prologue.c +++ b/tools/perf/tests/bpf-script-test-prologue.c @@ -6,7 +6,6 @@ # error Need LINUX_VERSION_CODE # error Example: for 4.2 kernel, put 'clang-opt="-DLINUX_VERSION_CODE=0x40200" into llvm section of ~/.perfconfig' #endif -#define SEC(NAME) __attribute__((section(NAME), used)) #include @@ -14,6 +13,9 @@ #define FMODE_WRITE 0x2 #ifndef BUILTIN_CLANG_DEFAULT_INCLUDE + +#define SEC(NAME) __attribute__((section(NAME), used)) + static void (*bpf_trace_printk)(const char *fmt, int fmt_size, ...) = (void *) 6; #endif diff --git a/tools/perf/tests/bpf-script-test-relocation.c b/tools/perf/tests/bpf-script-test-relocation.c index 57c96a3..bb54926 100644 --- a/tools/perf/tests/bpf-script-test-relocation.c +++ b/tools/perf/tests/bpf-script-test-relocation.c @@ -9,6 +9,7 @@ #define BPF_ANY 0 #define BPF_MAP_TYPE_ARRAY 2 +#ifndef BUILTIN_CLANG_DEFAULT_INCLUDE struct bpf_map_def { unsigned int type; unsigned int key_size; @@ -17,8 +18,6 @@ struct bpf_map_def { }; #define SEC(NAME) __attribute__((section(NAME), used)) - -#ifndef BUILTIN_CLANG_DEFAULT_INCLUDE #define BPF_FUNC_map_lookup_elem 1 #define BPF_FUNC_map_update_elem 2 diff --git a/tools/perf/util/c++/Build b/tools/perf/util/c++/Build index bd71abf..faa0268 100644 --- a/tools/perf/util/c++/Build +++ b/tools/perf/util/c++/Build @@ -1,3 +1,4 @@ libperf-$(CONFIG_CLANGLLVM) += clang.o libperf-$(CONFIG_CLANGLLVM) += clang-test.o libperf-$(CONFIG_CLANGLLVM) += bpf-funcs-str.o +libperf-$(CONFIG_CLANGLLVM) += bpf-helper-str.o diff --git a/tools/perf/util/c++/bpf-helper-str.c b/tools/perf/util/c++/bpf-helper-str.c new file mode 100644 index 0000000..17f915c --- /dev/null +++ b/tools/perf/util/c++/bpf-helper-str.c @@ -0,0 +1,15 @@ +#include "clang-bpf-includes.h" +const char clang_builtin_bpf_helper_str[] = +"#ifdef BUILTIN_CLANG_DEFAULT_INCLUDE\n" +"#ifndef BPF_HELPER_DEFINED\n" +"#define BPF_HELPER_DEFINED\n" +"struct bpf_map_def {\n" +" unsigned int type;\n" +" unsigned int key_size;\n" +" unsigned int value_size;\n" +" unsigned int max_entries;\n" +"};\n" +"#define SEC(NAME) __attribute__((section(NAME), used))\n" +"#endif\n" +"#endif" +; diff --git a/tools/perf/util/c++/clang-bpf-includes.h b/tools/perf/util/c++/clang-bpf-includes.h index 385a5bb..577b40c 100644 --- a/tools/perf/util/c++/clang-bpf-includes.h +++ b/tools/perf/util/c++/clang-bpf-includes.h @@ -5,6 +5,7 @@ extern "C" { #endif extern const char clang_builtin_bpf_funcs_str[]; +extern const char clang_builtin_bpf_helper_str[]; #ifdef __cplusplus } diff --git a/tools/perf/util/c++/clang.cpp b/tools/perf/util/c++/clang.cpp index feb69ea..d49305a 100644 --- a/tools/perf/util/c++/clang.cpp +++ b/tools/perf/util/c++/clang.cpp @@ -48,6 +48,7 @@ static struct BPFHeader { llvm::StringRef Content; } BPFHeaders[] = { {"/virtual/bpf-funcs.h", clang_builtin_bpf_funcs_str}, + {"/virtual/bpf-helper.h", clang_builtin_bpf_helper_str}, }; static std::unique_ptr LLVMCtx;