Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1279370

Re: [PATCH v2 02/13] bpf tools: Extract and collect map names from BPF object file

From Namhyung Kim <namhyung@kernel.org>
Newsgroups linux.kernel
Subject Re: [PATCH v2 02/13] bpf tools: Extract and collect map names from BPF object file
Date 2015-11-29 17:20 +0100
Message-ID <qAcRH-8lW-13@gated-at.bofh.it> (permalink)
References <qzmT8-uQ-3@gated-at.bofh.it> <qzmT8-uQ-1@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


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?


> +		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.

thanks,
Namhyung


> +		pr_debug("map %zu is \"%s\"\n", map_idx,
> +			 obj->maps[map_idx].name);
> +	}
> +}
> +
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCH v2 02/13] bpf tools: Extract and collect map names from BPF object file Wang Nan <wangnan0@huawei.com> - 2015-11-27 09:50 +0100
  [tip:perf/core] tools lib bpf:   Extract and collect map names from BPF object file tip-bot for Wang Nan <tipbot@zytor.com> - 2015-11-29 09:00 +0100
  Re: [PATCH v2 02/13] bpf tools: Extract and collect map names from  BPF object file Namhyung Kim <namhyung@kernel.org> - 2015-11-29 17:20 +0100
    Re: [PATCH v2 02/13] bpf tools: Extract and collect map names from  BPF object file "Wangnan (F)" <wangnan0@huawei.com> - 2015-11-30 06:10 +0100
      Re: [PATCH v2 02/13] bpf tools: Extract and collect map names from  BPF object file Namhyung Kim <namhyung@kernel.org> - 2015-11-30 10:00 +0100
        Re: [PATCH v2 02/13] bpf tools: Extract and collect map names from  BPF object file "Wangnan (F)" <wangnan0@huawei.com> - 2015-11-30 10:30 +0100
          Re: [PATCH v2 02/13] bpf tools: Extract and collect map names from BPF object file Namhyung Kim <namhyung@gmail.com> - 2015-11-30 10:50 +0100
            Re: [PATCH v2 02/13] bpf tools: Extract and collect map names from  BPF object file "Wangnan (F)" <wangnan0@huawei.com> - 2015-11-30 10:50 +0100
            [PATCH] tools lib bpf: Fetch map names from correct strtab Wang Nan <wangnan0@huawei.com> - 2015-11-30 11:40 +0100
              Re: [PATCH] tools lib bpf: Fetch map names from correct strtab Namhyung Kim <namhyung@kernel.org> - 2015-11-30 12:30 +0100

csiph-web