From patchwork Sat Nov 26 07:03:27 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wang Nan X-Patchwork-Id: 84246 Delivered-To: patch@linaro.org Received: by 10.140.20.101 with SMTP id 92csp386732qgi; Fri, 25 Nov 2016 23:10:44 -0800 (PST) X-Received: by 10.98.130.1 with SMTP id w1mr11509722pfd.35.1480144244769; Fri, 25 Nov 2016 23:10:44 -0800 (PST) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o61si19296393plb.168.2016.11.25.23.10.44; Fri, 25 Nov 2016 23:10:44 -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 S1752169AbcKZHKU (ORCPT + 25 others); Sat, 26 Nov 2016 02:10:20 -0500 Received: from szxga01-in.huawei.com ([58.251.152.64]:59836 "EHLO szxga01-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751082AbcKZHHQ (ORCPT ); Sat, 26 Nov 2016 02:07:16 -0500 Received: from 172.24.1.47 (EHLO szxeml431-hub.china.huawei.com) ([172.24.1.47]) by szxrg01-dlp.huawei.com (MOS 4.3.7-GA FastPath queued) with ESMTP id DVS32969; Sat, 26 Nov 2016 15:06:11 +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; Sat, 26 Nov 2016 15:06:01 +0800 From: Wang Nan To: , CC: , , , , , Wang Nan , Alexei Starovoitov , Jiri Olsa Subject: [PATCH v3 03/30] tools lib bpf: Retrive bpf_map through offset of bpf_map_def Date: Sat, 26 Nov 2016 07:03:27 +0000 Message-ID: <20161126070354.141764-4-wangnan0@huawei.com> X-Mailer: git-send-email 2.10.1 In-Reply-To: <20161126070354.141764-1-wangnan0@huawei.com> References: <20161126070354.141764-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 Add a new API to libbpf, caller is able to get bpf_map through the offset of bpf_map_def to 'maps' section. The API will be used to help jitted perf hook code find fd of a map. Signed-off-by: Wang Nan Cc: Arnaldo Carvalho de Melo Cc: Alexei Starovoitov Cc: Jiri Olsa Cc: Zefan Li Cc: pi3orama@163.com --- tools/lib/bpf/libbpf.c | 12 ++++++++++++ tools/lib/bpf/libbpf.h | 8 ++++++++ 2 files changed, 20 insertions(+) -- 2.10.1 Acked-by: Alexei Starovoitov diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 866d5cd..2e97459 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -1524,3 +1524,15 @@ bpf_object__find_map_by_name(struct bpf_object *obj, const char *name) } return NULL; } + +struct bpf_map * +bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset) +{ + int i; + + for (i = 0; i < obj->nr_maps; i++) { + if (obj->maps[i].offset == offset) + return &obj->maps[i]; + } + return ERR_PTR(-ENOENT); +} diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index 0c0b012..a5a8b86 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -24,6 +24,7 @@ #include #include #include +#include // for size_t enum libbpf_errno { __LIBBPF_ERRNO__START = 4000, @@ -200,6 +201,13 @@ struct bpf_map; struct bpf_map * bpf_object__find_map_by_name(struct bpf_object *obj, const char *name); +/* + * Get bpf_map through the offset of corresponding struct bpf_map_def + * in the bpf object file. + */ +struct bpf_map * +bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset); + struct bpf_map * bpf_map__next(struct bpf_map *map, struct bpf_object *obj); #define bpf_map__for_each(pos, obj) \