From patchwork Fri Sep 23 12:49:58 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wang Nan X-Patchwork-Id: 76849 Delivered-To: patch@linaro.org Received: by 10.140.106.72 with SMTP id d66csp552393qgf; Fri, 23 Sep 2016 05:51:08 -0700 (PDT) X-Received: by 10.66.88.227 with SMTP id bj3mr12258584pab.113.1474635068208; Fri, 23 Sep 2016 05:51:08 -0700 (PDT) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id if10si7839672pad.130.2016.09.23.05.51.07; Fri, 23 Sep 2016 05:51:08 -0700 (PDT) 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 S1759604AbcIWMvD (ORCPT + 27 others); Fri, 23 Sep 2016 08:51:03 -0400 Received: from szxga01-in.huawei.com ([58.251.152.64]:18955 "EHLO szxga01-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1759411AbcIWMu5 (ORCPT ); Fri, 23 Sep 2016 08:50:57 -0400 Received: from 172.24.1.136 (EHLO szxeml431-hub.china.huawei.com) ([172.24.1.136]) by szxrg01-dlp.huawei.com (MOS 4.3.7-GA FastPath queued) with ESMTP id DRL70268; Fri, 23 Sep 2016 20:50:21 +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; Fri, 23 Sep 2016 20:50:13 +0800 From: Wang Nan To: , CC: , , , Wang Nan , Arnaldo Carvalho de Melo , He Kuang , Jiri Olsa Subject: [PATCH 11/14] perf tools: Extract kernel build option detector as utils Date: Fri, 23 Sep 2016 12:49:58 +0000 Message-ID: <1474635001-153850-12-git-send-email-wangnan0@huawei.com> X-Mailer: git-send-email 1.8.3.4 In-Reply-To: <1474635001-153850-1-git-send-email-wangnan0@huawei.com> References: <1474635001-153850-1-git-send-email-wangnan0@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.107.193.248] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020204.57E5250E.002E, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2013-06-18 04:22:30, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: 5185028dd6412c6f4e90711597c99cd5 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Following commits will use builtin clang to compile BPF script. llvm__get_kbuild_opts() is extracted to help it detect kbuild dir and include options. Signed-off-by: Wang Nan Cc: Arnaldo Carvalho de Melo Cc: Alexei Starovoitov Cc: He Kuang Cc: Jiri Olsa --- tools/perf/util/llvm-utils.c | 44 ++++++++++++++++++++++++++++++++++++++------ tools/perf/util/llvm-utils.h | 3 +++ 2 files changed, 41 insertions(+), 6 deletions(-) -- 1.8.3.4 diff --git a/tools/perf/util/llvm-utils.c b/tools/perf/util/llvm-utils.c index bf7216b..d780a82 100644 --- a/tools/perf/util/llvm-utils.c +++ b/tools/perf/util/llvm-utils.c @@ -7,6 +7,7 @@ #include #include #include +#include #include "debug.h" #include "llvm-utils.h" #include "config.h" @@ -282,9 +283,10 @@ static const char *kinc_fetch_script = "rm -rf $TMPDIR\n" "exit $RET\n"; -static inline void -get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts) +void llvm__get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts) { + static char *saved_kbuild_dir; + static char *saved_kbuild_include_opts; int err; if (!kbuild_dir || !kbuild_include_opts) @@ -293,10 +295,28 @@ get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts) *kbuild_dir = NULL; *kbuild_include_opts = NULL; + if (saved_kbuild_dir && saved_kbuild_include_opts && + !IS_ERR(saved_kbuild_dir) && !IS_ERR(saved_kbuild_include_opts)) { + *kbuild_dir = strdup(saved_kbuild_dir); + *kbuild_include_opts = strdup(saved_kbuild_include_opts); + + if (*kbuild_dir && *kbuild_include_opts) + return; + + zfree(kbuild_dir); + zfree(kbuild_include_opts); + /* + * Don't fall through: it may breaks saved_kbuild_dir and + * saved_kbuild_include_opts if detect them again when + * memory is low. + */ + return; + } + if (llvm_param.kbuild_dir && !llvm_param.kbuild_dir[0]) { pr_debug("[llvm.kbuild-dir] is set to \"\" deliberately.\n"); pr_debug("Skip kbuild options detection.\n"); - return; + goto errout; } err = detect_kbuild_dir(kbuild_dir); @@ -306,7 +326,7 @@ get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts) "Hint:\tSet correct kbuild directory using 'kbuild-dir' option in [llvm]\n" " \tsection of ~/.perfconfig or set it to \"\" to suppress kbuild\n" " \tdetection.\n\n"); - return; + goto errout; } pr_debug("Kernel build dir is set to %s\n", *kbuild_dir); @@ -325,10 +345,22 @@ get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts) free(*kbuild_dir); *kbuild_dir = NULL; - return; + goto errout; } pr_debug("include option is set to %s\n", *kbuild_include_opts); + + saved_kbuild_dir = strdup(*kbuild_dir); + saved_kbuild_include_opts = strdup(*kbuild_include_opts); + + if (!saved_kbuild_dir || !saved_kbuild_include_opts) { + zfree(&saved_kbuild_dir); + zfree(&saved_kbuild_include_opts); + } + return; +errout: + saved_kbuild_dir = ERR_PTR(-EINVAL); + saved_kbuild_include_opts = ERR_PTR(-EINVAL); } static void @@ -406,7 +438,7 @@ int llvm__compile_bpf(const char *path, void **p_obj_buf, * This is an optional work. Even it fail we can continue our * work. Needn't to check error return. */ - get_kbuild_opts(&kbuild_dir, &kbuild_include_opts); + llvm__get_kbuild_opts(&kbuild_dir, &kbuild_include_opts); nr_cpus_avail = sysconf(_SC_NPROCESSORS_CONF); if (nr_cpus_avail <= 0) { diff --git a/tools/perf/util/llvm-utils.h b/tools/perf/util/llvm-utils.h index 9f501ce..d3f410d 100644 --- a/tools/perf/util/llvm-utils.h +++ b/tools/perf/util/llvm-utils.h @@ -50,4 +50,7 @@ int llvm__compile_bpf(const char *path, void **p_obj_buf, size_t *p_obj_buf_sz); /* This function is for test__llvm() use only */ int llvm__search_clang(void); + +/* This function is reused by builtin clang support */ +void llvm__get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts); #endif