diff mbox series

[dwarves,2/2] btf_encoder: generate BTF_KIND_TAG from llvm annotations

Message ID 20210920003555.3525533-1-yhs@fb.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series generate BTF_KIND_TAG types from DW_TAG_LLVM_annotation dwarf tags | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch
bpf/vmtest-bpf-PR fail merge-conflict
bpf/vmtest-bpf-next-PR fail merge-conflict

Commit Message

Yonghong Song Sept. 20, 2021, 12:35 a.m. UTC
The following is an example with latest upstream clang:
  $ cat t.c
  #define __tag1 __attribute__((btf_tag("tag1")))
  #define __tag2 __attribute__((btf_tag("tag2")))

  struct t {
          int a:1 __tag1;
          int b __tag2;
  } __tag1 __tag2;

  int g __tag1 __attribute__((section(".data..percpu")));

  int __tag1 foo(struct t *a1, int a2 __tag2) {
    return a1->b + a2 + g;
  }

  $ clang -O2 -g -c t.c
  $ pahole -JV t.o
  Found per-CPU symbol 'g' at address 0x0
  Found 1 per-CPU variables!
  Found 1 functions!
  File t.o:
  [1] INT int size=4 nr_bits=32 encoding=SIGNED
  [2] PTR (anon) type_id=3
  [3] STRUCT t size=8
        a type_id=1 bitfield_size=1 bits_offset=0
        b type_id=1 bitfield_size=0 bits_offset=32
  [4] TAG tag1 type_id=3 component_idx=0
  [5] TAG tag2 type_id=3 component_idx=1
  [6] TAG tag1 type_id=3 component_idx=-1
  [7] TAG tag2 type_id=3 component_idx=-1
  [8] FUNC_PROTO (anon) return=1 args=(2 a1, 1 a2)
  [9] FUNC foo type_id=8
  [10] TAG tag2 type_id=9 component_idx=1
  [11] TAG tag1 type_id=9 component_idx=-1
  search cu 't.c' for percpu global variables.
  Variable 'g' from CU 't.c' at address 0x0 encoded
  [12] VAR g type=1 linkage=1
  [13] TAG tag1 type_id=12 component_idx=-1
  [14] DATASEC .data..percpu size=4 vlen=1
        type=12 offset=0 size=4
  $ ...

With additional option --skip_encoding_btf_tag, pahole doesn't
generate BTF_KIND_TAGs any more.
  $ pahole -JV --skip_encoding_btf_tag t.o
  Found per-CPU symbol 'g' at address 0x0
  Found 1 per-CPU variables!
  Found 1 functions!
  File t.o:
  [1] INT int size=4 nr_bits=32 encoding=SIGNED
  [2] PTR (anon) type_id=3
  [3] STRUCT t size=8
        a type_id=1 bitfield_size=1 bits_offset=0
        b type_id=1 bitfield_size=0 bits_offset=32
  [4] FUNC_PROTO (anon) return=1 args=(2 a1, 1 a2)
  [5] FUNC foo type_id=4
  search cu 't.c' for percpu global variables.
  Variable 'g' from CU 't.c' at address 0x0 encoded
  [6] VAR g type=1 linkage=1
  [7] DATASEC .data..percpu size=4 vlen=1
        type=6 offset=0 size=4
  $ ...

Signed-off-by: Yonghong Song <yhs@fb.com>
---
 btf_encoder.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)

Comments

Andrii Nakryiko Sept. 21, 2021, 10:02 p.m. UTC | #1
On Sun, Sep 19, 2021 at 5:36 PM Yonghong Song <yhs@fb.com> wrote:
>
> The following is an example with latest upstream clang:
>   $ cat t.c
>   #define __tag1 __attribute__((btf_tag("tag1")))
>   #define __tag2 __attribute__((btf_tag("tag2")))
>
>   struct t {
>           int a:1 __tag1;
>           int b __tag2;
>   } __tag1 __tag2;
>
>   int g __tag1 __attribute__((section(".data..percpu")));
>
>   int __tag1 foo(struct t *a1, int a2 __tag2) {
>     return a1->b + a2 + g;
>   }
>
>   $ clang -O2 -g -c t.c
>   $ pahole -JV t.o
>   Found per-CPU symbol 'g' at address 0x0
>   Found 1 per-CPU variables!
>   Found 1 functions!
>   File t.o:
>   [1] INT int size=4 nr_bits=32 encoding=SIGNED
>   [2] PTR (anon) type_id=3
>   [3] STRUCT t size=8
>         a type_id=1 bitfield_size=1 bits_offset=0
>         b type_id=1 bitfield_size=0 bits_offset=32
>   [4] TAG tag1 type_id=3 component_idx=0
>   [5] TAG tag2 type_id=3 component_idx=1
>   [6] TAG tag1 type_id=3 component_idx=-1
>   [7] TAG tag2 type_id=3 component_idx=-1
>   [8] FUNC_PROTO (anon) return=1 args=(2 a1, 1 a2)
>   [9] FUNC foo type_id=8
>   [10] TAG tag2 type_id=9 component_idx=1
>   [11] TAG tag1 type_id=9 component_idx=-1
>   search cu 't.c' for percpu global variables.
>   Variable 'g' from CU 't.c' at address 0x0 encoded
>   [12] VAR g type=1 linkage=1
>   [13] TAG tag1 type_id=12 component_idx=-1
>   [14] DATASEC .data..percpu size=4 vlen=1
>         type=12 offset=0 size=4
>   $ ...
>
> With additional option --skip_encoding_btf_tag, pahole doesn't
> generate BTF_KIND_TAGs any more.
>   $ pahole -JV --skip_encoding_btf_tag t.o
>   Found per-CPU symbol 'g' at address 0x0
>   Found 1 per-CPU variables!
>   Found 1 functions!
>   File t.o:
>   [1] INT int size=4 nr_bits=32 encoding=SIGNED
>   [2] PTR (anon) type_id=3
>   [3] STRUCT t size=8
>         a type_id=1 bitfield_size=1 bits_offset=0
>         b type_id=1 bitfield_size=0 bits_offset=32
>   [4] FUNC_PROTO (anon) return=1 args=(2 a1, 1 a2)
>   [5] FUNC foo type_id=4
>   search cu 't.c' for percpu global variables.
>   Variable 'g' from CU 't.c' at address 0x0 encoded
>   [6] VAR g type=1 linkage=1
>   [7] DATASEC .data..percpu size=4 vlen=1
>         type=6 offset=0 size=4
>   $ ...
>
> Signed-off-by: Yonghong Song <yhs@fb.com>
> ---
>  btf_encoder.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 45 insertions(+)
>

[...]

> @@ -1244,6 +1266,10 @@ static int btf_encoder__encode_cu_variables(struct btf_encoder *encoder, struct
>                         goto out;
>                 }
>
> +               list_for_each_entry(annot, &var->annots, node) {
> +                       btf_encoder__add_tag(encoder, annot->value, id, annot->component_idx);

check errors?

> +               }
> +
>                 /*
>                  * add a BTF_VAR_SECINFO in encoder->percpu_secinfo, which will be added into
>                  * encoder->types later when we add BTF_VAR_DATASEC.
> @@ -1359,6 +1385,7 @@ void btf_encoder__delete(struct btf_encoder *encoder)
>  int btf_encoder__encode_cu(struct btf_encoder *encoder, struct cu *cu)
>  {
>         uint32_t type_id_off = btf__get_nr_types(encoder->btf);
> +       struct llvm_annotation *annot;
>         uint32_t core_id;
>         struct function *fn;
>         struct tag *pos;
> @@ -1396,6 +1423,20 @@ int btf_encoder__encode_cu(struct btf_encoder *encoder, struct cu *cu)
>                 encoder->has_index_type = true;
>         }
>
> +       cu__for_each_type(cu, core_id, pos) {
> +               struct namespace *ns;
> +               int btf_type_id;
> +
> +               if (pos->tag != DW_TAG_structure_type && pos->tag != DW_TAG_union_type)
> +                       continue;
> +
> +               btf_type_id = type_id_off + core_id;
> +               ns = tag__namespace(pos);
> +               list_for_each_entry(annot, &ns->annots, node) {
> +                       btf_encoder__add_tag(encoder, annot->value, btf_type_id, annot->component_idx);

same, this can fail

> +               }
> +       }
> +
>         cu__for_each_function(cu, core_id, fn) {
>                 int btf_fnproto_id, btf_fn_id;
>                 const char *name;
> @@ -1436,6 +1477,10 @@ int btf_encoder__encode_cu(struct btf_encoder *encoder, struct cu *cu)
>                         printf("error: failed to encode function '%s'\n", function__name(fn));
>                         goto out;
>                 }
> +
> +               list_for_each_entry(annot, &fn->annots, node) {
> +                       btf_encoder__add_tag(encoder, annot->value, btf_fn_id, annot->component_idx);

and here as well

> +               }
>         }
>
>         if (!encoder->skip_encoding_vars)
> --
> 2.30.2
>
Yonghong Song Sept. 22, 2021, 1:16 a.m. UTC | #2
On 9/21/21 3:02 PM, Andrii Nakryiko wrote:
> On Sun, Sep 19, 2021 at 5:36 PM Yonghong Song <yhs@fb.com> wrote:
>>
>> The following is an example with latest upstream clang:
>>    $ cat t.c
>>    #define __tag1 __attribute__((btf_tag("tag1")))
>>    #define __tag2 __attribute__((btf_tag("tag2")))
>>
>>    struct t {
>>            int a:1 __tag1;
>>            int b __tag2;
>>    } __tag1 __tag2;
>>
>>    int g __tag1 __attribute__((section(".data..percpu")));
>>
>>    int __tag1 foo(struct t *a1, int a2 __tag2) {
>>      return a1->b + a2 + g;
>>    }
>>
>>    $ clang -O2 -g -c t.c
>>    $ pahole -JV t.o
>>    Found per-CPU symbol 'g' at address 0x0
>>    Found 1 per-CPU variables!
>>    Found 1 functions!
>>    File t.o:
>>    [1] INT int size=4 nr_bits=32 encoding=SIGNED
>>    [2] PTR (anon) type_id=3
>>    [3] STRUCT t size=8
>>          a type_id=1 bitfield_size=1 bits_offset=0
>>          b type_id=1 bitfield_size=0 bits_offset=32
>>    [4] TAG tag1 type_id=3 component_idx=0
>>    [5] TAG tag2 type_id=3 component_idx=1
>>    [6] TAG tag1 type_id=3 component_idx=-1
>>    [7] TAG tag2 type_id=3 component_idx=-1
>>    [8] FUNC_PROTO (anon) return=1 args=(2 a1, 1 a2)
>>    [9] FUNC foo type_id=8
>>    [10] TAG tag2 type_id=9 component_idx=1
>>    [11] TAG tag1 type_id=9 component_idx=-1
>>    search cu 't.c' for percpu global variables.
>>    Variable 'g' from CU 't.c' at address 0x0 encoded
>>    [12] VAR g type=1 linkage=1
>>    [13] TAG tag1 type_id=12 component_idx=-1
>>    [14] DATASEC .data..percpu size=4 vlen=1
>>          type=12 offset=0 size=4
>>    $ ...
>>
>> With additional option --skip_encoding_btf_tag, pahole doesn't
>> generate BTF_KIND_TAGs any more.
>>    $ pahole -JV --skip_encoding_btf_tag t.o
>>    Found per-CPU symbol 'g' at address 0x0
>>    Found 1 per-CPU variables!
>>    Found 1 functions!
>>    File t.o:
>>    [1] INT int size=4 nr_bits=32 encoding=SIGNED
>>    [2] PTR (anon) type_id=3
>>    [3] STRUCT t size=8
>>          a type_id=1 bitfield_size=1 bits_offset=0
>>          b type_id=1 bitfield_size=0 bits_offset=32
>>    [4] FUNC_PROTO (anon) return=1 args=(2 a1, 1 a2)
>>    [5] FUNC foo type_id=4
>>    search cu 't.c' for percpu global variables.
>>    Variable 'g' from CU 't.c' at address 0x0 encoded
>>    [6] VAR g type=1 linkage=1
>>    [7] DATASEC .data..percpu size=4 vlen=1
>>          type=6 offset=0 size=4
>>    $ ...
>>
>> Signed-off-by: Yonghong Song <yhs@fb.com>
>> ---
>>   btf_encoder.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
>>   1 file changed, 45 insertions(+)
>>
> 
> [...]
> 
>> @@ -1244,6 +1266,10 @@ static int btf_encoder__encode_cu_variables(struct btf_encoder *encoder, struct
>>                          goto out;
>>                  }
>>
>> +               list_for_each_entry(annot, &var->annots, node) {
>> +                       btf_encoder__add_tag(encoder, annot->value, id, annot->component_idx);
> 
> check errors?

Yes, I missed it. Will fix this and the following two other instances 
and send v2.

> 
>> +               }
>> +
>>                  /*
>>                   * add a BTF_VAR_SECINFO in encoder->percpu_secinfo, which will be added into
>>                   * encoder->types later when we add BTF_VAR_DATASEC.
>> @@ -1359,6 +1385,7 @@ void btf_encoder__delete(struct btf_encoder *encoder)
>>   int btf_encoder__encode_cu(struct btf_encoder *encoder, struct cu *cu)
>>   {
>>          uint32_t type_id_off = btf__get_nr_types(encoder->btf);
>> +       struct llvm_annotation *annot;
>>          uint32_t core_id;
>>          struct function *fn;
>>          struct tag *pos;
>> @@ -1396,6 +1423,20 @@ int btf_encoder__encode_cu(struct btf_encoder *encoder, struct cu *cu)
>>                  encoder->has_index_type = true;
>>          }
>>
>> +       cu__for_each_type(cu, core_id, pos) {
>> +               struct namespace *ns;
>> +               int btf_type_id;
>> +
>> +               if (pos->tag != DW_TAG_structure_type && pos->tag != DW_TAG_union_type)
>> +                       continue;
>> +
>> +               btf_type_id = type_id_off + core_id;
>> +               ns = tag__namespace(pos);
>> +               list_for_each_entry(annot, &ns->annots, node) {
>> +                       btf_encoder__add_tag(encoder, annot->value, btf_type_id, annot->component_idx);
> 
> same, this can fail
> 
>> +               }
>> +       }
>> +
>>          cu__for_each_function(cu, core_id, fn) {
>>                  int btf_fnproto_id, btf_fn_id;
>>                  const char *name;
>> @@ -1436,6 +1477,10 @@ int btf_encoder__encode_cu(struct btf_encoder *encoder, struct cu *cu)
>>                          printf("error: failed to encode function '%s'\n", function__name(fn));
>>                          goto out;
>>                  }
>> +
>> +               list_for_each_entry(annot, &fn->annots, node) {
>> +                       btf_encoder__add_tag(encoder, annot->value, btf_fn_id, annot->component_idx);
> 
> and here as well
> 
>> +               }
>>          }
>>
>>          if (!encoder->skip_encoding_vars)
>> --
>> 2.30.2
>>
diff mbox series

Patch

diff --git a/btf_encoder.c b/btf_encoder.c
index 1b4e83d..e983750 100644
--- a/btf_encoder.c
+++ b/btf_encoder.c
@@ -141,6 +141,7 @@  static const char * const btf_kind_str[NR_BTF_KINDS] = {
 	[BTF_KIND_VAR]          = "VAR",
 	[BTF_KIND_DATASEC]      = "DATASEC",
 	[BTF_KIND_FLOAT]        = "FLOAT",
+	[BTF_KIND_TAG]          = "TAG",
 };
 
 static const char *btf__printable_name(const struct btf *btf, uint32_t offset)
@@ -644,6 +645,26 @@  static int32_t btf_encoder__add_datasec(struct btf_encoder *encoder, const char
 	return id;
 }
 
+static int32_t btf_encoder__add_tag(struct btf_encoder *encoder, const char *value, uint32_t type,
+				    int component_idx)
+{
+	struct btf *btf = encoder->btf;
+	const struct btf_type *t;
+	int32_t id;
+
+	id = btf__add_tag(btf, value, type, component_idx);
+	if (id > 0) {
+		t = btf__type_by_id(btf, id);
+		btf_encoder__log_type(encoder, t, false, true, "type_id=%u component_idx=%d",
+				      t->type, component_idx);
+	} else {
+		btf__log_err(btf, BTF_KIND_TAG, value, true, "component_idx=%d Error emitting BTF type",
+			     component_idx);
+	}
+
+	return id;
+}
+
 /*
  * This corresponds to the same macro defined in
  * include/linux/kallsyms.h
@@ -1158,6 +1179,7 @@  static int btf_encoder__encode_cu_variables(struct btf_encoder *encoder, struct
 		struct variable *var = tag__variable(pos);
 		uint32_t size, type, linkage;
 		const char *name, *dwarf_name;
+		struct llvm_annotation *annot;
 		const struct tag *tag;
 		uint64_t addr;
 		int id;
@@ -1244,6 +1266,10 @@  static int btf_encoder__encode_cu_variables(struct btf_encoder *encoder, struct
 			goto out;
 		}
 
+		list_for_each_entry(annot, &var->annots, node) {
+			btf_encoder__add_tag(encoder, annot->value, id, annot->component_idx);
+		}
+
 		/*
 		 * add a BTF_VAR_SECINFO in encoder->percpu_secinfo, which will be added into
 		 * encoder->types later when we add BTF_VAR_DATASEC.
@@ -1359,6 +1385,7 @@  void btf_encoder__delete(struct btf_encoder *encoder)
 int btf_encoder__encode_cu(struct btf_encoder *encoder, struct cu *cu)
 {
 	uint32_t type_id_off = btf__get_nr_types(encoder->btf);
+	struct llvm_annotation *annot;
 	uint32_t core_id;
 	struct function *fn;
 	struct tag *pos;
@@ -1396,6 +1423,20 @@  int btf_encoder__encode_cu(struct btf_encoder *encoder, struct cu *cu)
 		encoder->has_index_type = true;
 	}
 
+	cu__for_each_type(cu, core_id, pos) {
+		struct namespace *ns;
+		int btf_type_id;
+
+		if (pos->tag != DW_TAG_structure_type && pos->tag != DW_TAG_union_type)
+			continue;
+
+		btf_type_id = type_id_off + core_id;
+		ns = tag__namespace(pos);
+		list_for_each_entry(annot, &ns->annots, node) {
+			btf_encoder__add_tag(encoder, annot->value, btf_type_id, annot->component_idx);
+		}
+	}
+
 	cu__for_each_function(cu, core_id, fn) {
 		int btf_fnproto_id, btf_fn_id;
 		const char *name;
@@ -1436,6 +1477,10 @@  int btf_encoder__encode_cu(struct btf_encoder *encoder, struct cu *cu)
 			printf("error: failed to encode function '%s'\n", function__name(fn));
 			goto out;
 		}
+
+		list_for_each_entry(annot, &fn->annots, node) {
+			btf_encoder__add_tag(encoder, annot->value, btf_fn_id, annot->component_idx);
+		}
 	}
 
 	if (!encoder->skip_encoding_vars)