From patchwork Tue Dec 6 07:13:53 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wang Nan X-Patchwork-Id: 86684 Delivered-To: patch@linaro.org Received: by 10.140.20.101 with SMTP id 92csp1880277qgi; Mon, 5 Dec 2016 23:15:14 -0800 (PST) X-Received: by 10.99.138.75 with SMTP id y72mr109252246pgd.90.1481008514385; Mon, 05 Dec 2016 23:15:14 -0800 (PST) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 90si18316097pla.214.2016.12.05.23.15.13; Mon, 05 Dec 2016 23:15:14 -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 S1752763AbcLFHOz (ORCPT + 25 others); Tue, 6 Dec 2016 02:14:55 -0500 Received: from szxga02-in.huawei.com ([119.145.14.65]:46225 "EHLO szxga02-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752609AbcLFHOr (ORCPT ); Tue, 6 Dec 2016 02:14:47 -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 DRT24874; 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:14 +0800 From: Wang Nan To: CC: , , Wang Nan , Arnaldo Carvalho de Melo , "Alexei Starovoitov" , He Kuang , Jiri Olsa , Zefan Li , Subject: [PATCH v4 15/18] perf clang: Declare BPF functions for BPF scripts automatically Date: Tue, 6 Dec 2016 07:13:53 +0000 Message-ID: <20161206071356.5312-16-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 Use Clang's OverlayFileSystem, add '-include' options to make builtin clang define BPF functions. After this patch BPF script writer needn't define BPF functions by their own. Add -DBUILTIN_CLANG_DEFAULT_INCLUDE to builtin clang so when adopting builtin clang BPF functions can be automatically defined, and keep undefined when using external clang. Passing a -UBUILTIN_CLANG_DEFAULT_INCLUDE to cancel this defaudefinition. Test cases are updated to avoid redefinition of these functions. 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 | 18 +++++++++------ tools/perf/tests/bpf-script-test-prologue.c | 2 ++ tools/perf/tests/bpf-script-test-relocation.c | 18 +++++++++------ tools/perf/util/c++/clang.cpp | 33 ++++++++++++++++++++++++++- 4 files changed, 56 insertions(+), 15 deletions(-) -- 2.10.1 diff --git a/tools/perf/tests/bpf-script-example.c b/tools/perf/tests/bpf-script-example.c index ccbc19c..42dc341 100644 --- a/tools/perf/tests/bpf-script-example.c +++ b/tools/perf/tests/bpf-script-example.c @@ -8,13 +8,6 @@ #endif #define BPF_ANY 0 #define BPF_MAP_TYPE_ARRAY 2 -#define BPF_FUNC_map_lookup_elem 1 -#define BPF_FUNC_map_update_elem 2 - -static void *(*bpf_map_lookup_elem)(void *map, void *key) = - (void *) BPF_FUNC_map_lookup_elem; -static void *(*bpf_map_update_elem)(void *map, void *key, void *value, int flags) = - (void *) BPF_FUNC_map_update_elem; struct bpf_map_def { unsigned int type; @@ -24,6 +17,17 @@ 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 + +static void *(*bpf_map_lookup_elem)(void *map, void *key) = + (void *) BPF_FUNC_map_lookup_elem; +static void *(*bpf_map_update_elem)(void *map, void *key, void *value, int flags) = + (void *) BPF_FUNC_map_update_elem; +#endif + struct bpf_map_def SEC("maps") flip_table = { .type = BPF_MAP_TYPE_ARRAY, .key_size = sizeof(int), diff --git a/tools/perf/tests/bpf-script-test-prologue.c b/tools/perf/tests/bpf-script-test-prologue.c index 7230e62..ada812b 100644 --- a/tools/perf/tests/bpf-script-test-prologue.c +++ b/tools/perf/tests/bpf-script-test-prologue.c @@ -13,8 +13,10 @@ #define FMODE_READ 0x1 #define FMODE_WRITE 0x2 +#ifndef BUILTIN_CLANG_DEFAULT_INCLUDE static void (*bpf_trace_printk)(const char *fmt, int fmt_size, ...) = (void *) 6; +#endif SEC("func=null_lseek file->f_mode offset orig") int bpf_func__null_lseek(void *ctx, int err, unsigned long f_mode, diff --git a/tools/perf/tests/bpf-script-test-relocation.c b/tools/perf/tests/bpf-script-test-relocation.c index 93af774..57c96a3 100644 --- a/tools/perf/tests/bpf-script-test-relocation.c +++ b/tools/perf/tests/bpf-script-test-relocation.c @@ -8,13 +8,6 @@ #endif #define BPF_ANY 0 #define BPF_MAP_TYPE_ARRAY 2 -#define BPF_FUNC_map_lookup_elem 1 -#define BPF_FUNC_map_update_elem 2 - -static void *(*bpf_map_lookup_elem)(void *map, void *key) = - (void *) BPF_FUNC_map_lookup_elem; -static void *(*bpf_map_update_elem)(void *map, void *key, void *value, int flags) = - (void *) BPF_FUNC_map_update_elem; struct bpf_map_def { unsigned int type; @@ -24,6 +17,17 @@ 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 + +static void *(*bpf_map_lookup_elem)(void *map, void *key) = + (void *) BPF_FUNC_map_lookup_elem; +static void *(*bpf_map_update_elem)(void *map, void *key, void *value, int flags) = + (void *) BPF_FUNC_map_update_elem; +#endif + struct bpf_map_def SEC("maps") my_table = { .type = BPF_MAP_TYPE_ARRAY, .key_size = sizeof(int), diff --git a/tools/perf/util/c++/clang.cpp b/tools/perf/util/c++/clang.cpp index 1bd92ea..feb69ea 100644 --- a/tools/perf/util/c++/clang.cpp +++ b/tools/perf/util/c++/clang.cpp @@ -39,9 +39,17 @@ #include "util-cxx.h" #include "perf-hooks.h" #include "jit-helpers.h" +#include "clang-bpf-includes.h" namespace perf { +static struct BPFHeader { + llvm::StringRef Path; + llvm::StringRef Content; +} BPFHeaders[] = { + {"/virtual/bpf-funcs.h", clang_builtin_bpf_funcs_str}, +}; + static std::unique_ptr LLVMCtx; using namespace clang; @@ -63,6 +71,11 @@ createCompilerInvocation(llvm::opt::ArgStringList CFlags, StringRef& Path, "-x", "c"}; CCArgs.append(CFlags.begin(), CFlags.end()); + for (BPFHeader &h : BPFHeaders) { + CCArgs.append(1, "-include"); + CCArgs.append(1, h.Path.begin()); + } + CompilerInvocation *CI = tooling::newInvocation(&Diags, CCArgs); FrontendOptions& Opts = CI->getFrontendOpts(); @@ -71,6 +84,22 @@ createCompilerInvocation(llvm::opt::ArgStringList CFlags, StringRef& Path, return CI; } +static IntrusiveRefCntPtr +addBPFHeaders(IntrusiveRefCntPtr VFS) +{ + using namespace vfs; + + llvm::IntrusiveRefCntPtr OverlayFS( + new OverlayFileSystem(VFS)); + llvm::IntrusiveRefCntPtr MemFS( + new InMemoryFileSystem(true)); + OverlayFS->pushOverlay(MemFS); + + for (BPFHeader &h : BPFHeaders) + MemFS->addFile(h.Path, 0, llvm::MemoryBuffer::getMemBuffer(h.Content)); + return OverlayFS; +} + static std::unique_ptr getModuleFromSource(llvm::opt::ArgStringList CFlags, StringRef Path, IntrusiveRefCntPtr VFS) @@ -78,7 +107,8 @@ getModuleFromSource(llvm::opt::ArgStringList CFlags, CompilerInstance Clang; Clang.createDiagnostics(); - Clang.setVirtualFileSystem(&*VFS); + IntrusiveRefCntPtr OverlayVFS = addBPFHeaders(VFS); + Clang.setVirtualFileSystem(&*OverlayVFS); IntrusiveRefCntPtr CI = createCompilerInvocation(std::move(CFlags), Path, @@ -366,6 +396,7 @@ public: { CFlags.push_back(KVerDef.c_str()); CFlags.push_back(NRCpusDef.c_str()); + CFlags.push_back("-DBUILTIN_CLANG_DEFAULT_INCLUDE"); fillCFlagsFromString(CFlags, clang_opt); fillCFlagsFromString(CFlags, kbuild_include_opts, true);