From: "Wangnan (F)" <wangnan0@huawei.com>
To: Namhyung Kim <namhyung@kernel.org>
Cc: <acme@kernel.org>, <ast@kernel.org>,
<linux-kernel@vger.kernel.org>, <masami.hiramatsu.pt@hitachi.com>,
<lizefan@huawei.com>, <pi3orama@163.com>,
He Kuang <hekuang@huawei.com>,
Arnaldo Carvalho de Melo <acme@redhat.com>
Subject: Re: [PATCH v2 02/13] bpf tools: Extract and collect map names from BPF object file
Date: Mon, 30 Nov 2015 13:00:46 +0800 [thread overview]
Message-ID: <565BD7FE.50405@huawei.com> (raw)
In-Reply-To: <20151129161434.GE16382@danjae.kornet>
On 2015/11/30 0:14, Namhyung Kim wrote:
> Hi Wang,
>
> On Fri, Nov 27, 2015 at 08:47:36AM +0000, Wang Nan wrote:
>> This patch collects name of maps in BPF object files and saves them into
>> 'maps' field in 'struct bpf_object'. 'bpf_object__get_map_by_name' is
>> introduced to retrive fd and definitions of a map through its name.
>>
>> Signed-off-by: Wang Nan <wangnan0@huawei.com>
>> Signed-off-by: He Kuang <hekuang@huawei.com>
>> Cc: Alexei Starovoitov <ast@kernel.org>
>> Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
>> Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
>> Cc: Namhyung Kim <namhyung@kernel.org>
>> Cc: Zefan Li <lizefan@huawei.com>
>> Cc: pi3orama@163.com
>> ---
>> tools/lib/bpf/libbpf.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++---
>> tools/lib/bpf/libbpf.h | 3 +++
>> 2 files changed, 65 insertions(+), 3 deletions(-)
>>
>> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
>> index f509825..a298614 100644
>> --- a/tools/lib/bpf/libbpf.c
>> +++ b/tools/lib/bpf/libbpf.c
>> @@ -165,6 +165,7 @@ struct bpf_program {
>>
>> struct bpf_map {
>> int fd;
>> + char *name;
>> struct bpf_map_def def;
>> void *priv;
>> bpf_map_clear_priv_t clear_priv;
>> @@ -526,12 +527,46 @@ bpf_object__init_maps(struct bpf_object *obj, void *data,
>> return 0;
>> }
>>
>> +static void
>> +bpf_object__init_maps_name(struct bpf_object *obj, int maps_shndx)
>> +{
>> + int i;
>> + Elf_Data *symbols = obj->efile.symbols;
>> +
>> + if (!symbols || maps_shndx < 0)
>> + return;
>> +
>> + for (i = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
>> + GElf_Sym sym;
>> + size_t map_idx;
>> + const char *map_name;
>> +
>> + if (!gelf_getsym(symbols, i, &sym))
>> + continue;
>> + if (sym.st_shndx != maps_shndx)
>> + continue;
>> +
>> + map_name = elf_strptr(obj->efile.elf,
>> + obj->efile.ehdr.e_shstrndx,
>> + sym.st_name);
> It means that each map name is saved in section header string table?
According to elf format specification:
For an symbol table entry, the st_name field "holds an index
into the object file’s symbol string table, which holds the
character representations of the symbol names. If the value
is non-zero, it represents a string table index that gives
the symbol name. Otherwise, the symbol table entry has no
name."
And so called "object file’s symbol string table" is a
section in the object file which index is stored into
ehdr and be loaded during gelf_getehdr(), and its index
would be set to ehdr->e_shstrndx. So I think for each map
its name should be saved in that string table.
>
>> + map_idx = sym.st_value / sizeof(struct bpf_map_def);
>> + if (map_idx >= obj->nr_maps) {
>> + pr_warning("index of map \"%s\" is buggy: %zu > %zu\n",
>> + map_name, map_idx, obj->nr_maps);
>> + continue;
>> + }
>> + obj->maps[map_idx].name = strdup(map_name);
> You need to check the return value.
Will send a patch for it.
Thank you.
next prev parent reply other threads:[~2015-11-30 5:01 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-11-27 8:47 [PATCH v2 00/13] perf tools: BPF related update Wang Nan
2015-11-27 8:47 ` [PATCH v2 01/13] bpf tools: Collect map definition in bpf_object Wang Nan
2015-11-29 7:57 ` [tip:perf/core] tools lib bpf: " tip-bot for Wang Nan
2015-11-27 8:47 ` [PATCH v2 02/13] bpf tools: Extract and collect map names from BPF object file Wang Nan
2015-11-29 7:57 ` [tip:perf/core] tools lib bpf: " tip-bot for Wang Nan
2015-11-29 16:14 ` [PATCH v2 02/13] bpf tools: " Namhyung Kim
2015-11-30 5:00 ` Wangnan (F) [this message]
2015-11-30 8:51 ` Namhyung Kim
2015-11-30 9:27 ` Wangnan (F)
2015-11-30 9:43 ` Namhyung Kim
2015-11-30 9:48 ` Wangnan (F)
2015-11-30 10:39 ` [PATCH] tools lib bpf: Fetch map names from correct strtab Wang Nan
2015-11-30 11:19 ` Namhyung Kim
2015-11-27 8:47 ` [PATCH v2 03/13] perf tools: Rename bpf config to program config Wang Nan
2015-11-29 7:58 ` [tip:perf/core] perf bpf: " tip-bot for Wang Nan
2015-11-27 8:47 ` [PATCH v2 04/13] perf tools: Add API to config maps in bpf object Wang Nan
2015-11-28 1:10 ` RFC " Arnaldo Carvalho de Melo
2015-11-28 1:20 ` Wangnan (F)
2015-11-28 1:21 ` Wangnan (F)
2015-11-29 16:21 ` Namhyung Kim
2015-11-27 8:47 ` [PATCH v2 05/13] perf tools: Enable BPF object configure syntax Wang Nan
2015-11-27 8:47 ` [PATCH v2 06/13] perf record: Apply config to BPF objects before recording Wang Nan
2015-11-27 8:47 ` [PATCH v2 07/13] perf tools: Support perf event alias name Wang Nan
2015-11-27 8:47 ` [PATCH v2 08/13] perf tools: Enable passing event to BPF object Wang Nan
2015-11-27 8:47 ` [PATCH v2 09/13] perf tools: Support setting different slots in a BPF map separately Wang Nan
2015-11-27 8:47 ` [PATCH v2 10/13] perf tools: Enable indices setting syntax for BPF maps Wang Nan
2015-11-27 8:47 ` [PATCH v2 11/13] perf tools: Introduce bpf-output event Wang Nan
2015-11-27 8:47 ` [PATCH v2 12/13] perf data: Add u32_hex data type Wang Nan
2015-11-27 8:47 ` [PATCH v2 13/13] perf data: Support converting data from bpf_perf_event_output() Wang Nan
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=565BD7FE.50405@huawei.com \
--to=wangnan0@huawei.com \
--cc=acme@kernel.org \
--cc=acme@redhat.com \
--cc=ast@kernel.org \
--cc=hekuang@huawei.com \
--cc=linux-kernel@vger.kernel.org \
--cc=lizefan@huawei.com \
--cc=masami.hiramatsu.pt@hitachi.com \
--cc=namhyung@kernel.org \
--cc=pi3orama@163.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).