Re: [PATCH] btf: improve -dA comments for testsuite

2023-05-30 Thread Indu Bhagat via Gcc-patches

On 5/30/23 09:08, David Faust wrote:

@@ -793,7 +917,8 @@ btf_asm_enum_const (unsigned int size, ctf_dmdef_t * dmd)
   /* Asm'out a function parameter description following a BTF_KIND_FUNC_PROTO. 
 */
   
   static void

-btf_asm_func_arg (ctf_func_arg_t * farg, size_t stroffset)
+btf_asm_func_arg (ctf_container_ref ctfc, ctf_func_arg_t * farg,
+ size_t stroffset)
   {
 /* If the function arg does not have a name, refer to the null string at
the start of the string table. This ensures correct encoding for varargs
@@ -803,31 +928,33 @@ btf_asm_func_arg (ctf_func_arg_t * farg, size_t stroffset)
 else
   dw2_asm_output_data (4, 0, "farg_name");
   
-  dw2_asm_output_data (4, (btf_removed_type_p (farg->farg_type)

-  ? BTF_VOID_TYPEID
-  : get_btf_id (farg->farg_type)),
-  "farg_type");
+  btf_asm_type_ref ("farg_type", ctfc, (btf_removed_type_p (farg->farg_type)
+   ? BTF_VOID_TYPEID
+   : get_btf_id (farg->farg_type)));
   }
   
   /* Asm'out a BTF_KIND_FUNC type.  */
   

Lets keep the function level comments updated.  Apart from
btf_asm_func_type, this comment applies to other functions touched in
this commit, like btf_asm_datasec_type.

I don't follow. All those functions are still doing the same thing.
What needs to be updated exactly?



I meant updating the function-level comments for the additional args 
that are added.


I saw that in this patch, the new functions added follow the style of 
explaining the args, like,


+/* Return the relative index of the variable with final BTF ID ABS.  */
+
+static ctf_id_t
+btf_relative_var_id (ctf_id_t abs)

Hence, my comment.  But on second look, however, I see that the file 
keeps a mix of different styles. This one is up to you then. It makes 
sense to leave out the self-explanatory args.



   static void
-btf_asm_func_type (ctf_dtdef_ref dtd)
+btf_asm_func_type (ctf_container_ref ctfc, ctf_dtdef_ref dtd, size_t i)
   {
-  dw2_asm_output_data (4, dtd->dtd_data.ctti_name, "btt_name");
-  dw2_asm_output_data (4, BTF_TYPE_INFO (BTF_KIND_FUNC, 0,
- dtd->linkage),
-   "btt_info: kind=%u, kflag=%u, linkage=%u",
-   BTF_KIND_FUNC, 0, dtd->linkage);
-  dw2_asm_output_data (4, get_btf_id (dtd->dtd_data.ctti_type), "btt_type");
+  ctf_id_t ref_id = dtd->dtd_data.ctti_type;
+  dw2_asm_output_data (4, dtd->dtd_data.ctti_name,
+  "TYPE %lu BTF_KIND_FUNC '%s'",
+  num_types_added + num_vars_added + 1 + i,
+  dtd->dtd_name);
+  dw2_asm_output_data (4, BTF_TYPE_INFO (BTF_KIND_FUNC, 0, dtd->linkage),
+  "btt_info: kind=%u, kflag=%u, linkage=%u",
+  BTF_KIND_FUNC, 0, dtd->linkage);
+  btf_asm_type_ref ("btt_type", ctfc, get_btf_id (ref_id));
   }
   
   /* Asm'out a variable entry following a BTF_KIND_DATASEC.  */
   
   static void

-btf_asm_datasec_entry (struct btf_var_secinfo info)
+btf_asm_datasec_entry (ctf_container_ref ctfc, struct btf_var_secinfo info)
   {
-  dw2_asm_output_data (4, info.type, "bts_type");
+  btf_asm_type_ref ("bts_type", ctfc, info.type);
 dw2_asm_output_data (4, info.offset, "bts_offset");
 dw2_asm_output_data (4, info.size, "bts_size");
   }
@@ -835,9 +962,12 @@ btf_asm_datasec_entry (struct btf_var_secinfo info)
   /* Asm'out a whole BTF_KIND_DATASEC, including its variable entries.  */
   
   static void

-btf_asm_datasec_type (btf_datasec_t ds, size_t stroffset)
+btf_asm_datasec_type (ctf_container_ref ctfc, btf_datasec_t ds, ctf_id_t id,
+ size_t stroffset)
   {
-  dw2_asm_output_data (4, ds.name_offset + stroffset, "btt_name");
+  dw2_asm_output_data (4, ds.name_offset + stroffset,
+  "TYPE %lu BTF_KIND_DATASEC '%s'",
+  btf_absolute_datasec_id (id), ds.name);
 dw2_asm_output_data (4, BTF_TYPE_INFO (BTF_KIND_DATASEC, 0,
 ds.entries.length ()),
   "btt_info");
@@ -845,7 +975,7 @@ btf_asm_datasec_type (btf_datasec_t ds, size_t stroffset)
loaders such as libbpf.  */
 dw2_asm_output_data (4, 0, "btt_size");
 for (size_t i = 0; i < ds.entries.length (); i++)
-btf_asm_datasec_entry (ds.entries[i]);
+btf_asm_datasec_entry (ctfc, ds.entries[i]);
   }
   




Re: [PATCH] btf: improve -dA comments for testsuite

2023-05-30 Thread David Faust via Gcc-patches



On 5/30/23 00:30, Indu Bhagat wrote:
> On 5/25/23 9:37 AM, David Faust via Gcc-patches wrote:
>> Many BTF type kinds refer to other types via index to the final types
>> list. However, the order of the final types list is not guaranteed to
>> remain the same for the same source program between different runs of
>> the compiler, making it difficult to test inter-type references.
>>
>> This patch updates the assembler comments output when writing a
>> given BTF record to include minimal information about the referenced
>> type, if any. This allows for the regular expressions used in the gcc
>> testsuite to do some basic integrity checks on inter-type references.
>>
>> For example, for the type
>>
>>  unsigned int *
>>
>> Assembly comments like the following are written with -dA:
>>
>>  .4byte  0   ; TYPE 2 BTF_KIND_PTR ''
>>  .4byte  0x200   ; btt_info: kind=2, kflag=0, vlen=0
>>  .4byte  0x1 ; btt_type: (BTF_KIND_INT 'unsigned int')
>>
>> Several BTF tests which can immediately be made more robust with this
>> change are updated. It will also be useful in new tests for the upcoming
>> btf_type_tag support.
>>
> 
> Thanks for working on this, David.  It will be nice to use these 
> enhanced assembler comments in the output for some of CTF testing as 
> well sometime.  But we can get to that later after this comit.
> 
> Some comments inlined below.
> 
>> Tested on BPF and x86_64, no known regressions.
>> OK for trunk?
>>
>> Thanks.
>>
>> gcc/
>>
>>  * btfout.cc (btf_kind_names): New.
>>  (btf_kind_name): New.
>>  (btf_absolute_var_id): New utility function.
>>  (btf_relative_var_id): Likewise.
>>  (btf_relative_func_id): Likewise.
>>  (btf_absolute_datasec_id): Likewise.
>>  (btf_asm_type_ref): New.
>>  (btf_asm_type): Update asm comments and use btf_asm_type_ref ().
>>  (btf_asm_array): Likewise. Accept ctf_container_ref parameter.
>>  (btf_asm_varent): Likewise.
>>  (btf_asm_func_arg): Likewise.
>>  (btf_asm_datasec_entry): Likewise.
>>  (btf_asm_datasec_type): Likewise.
>>  (btf_asm_func_type): Likewise. Add index parameter.
>>  (btf_asm_sou_member): Likewise.
>>  (output_btf_vars): Update btf_asm_* call accordingly.
>>  (output_asm_btf_sou_fields): Likewise.
>>  (output_asm_btf_func_args_list): Likewise.
>>  (output_asm_btf_vlen_bytes): Likewise.
>>  (output_btf_func_types): Add ctf_container_ref parameter.
>>  Pass it to btf_asm_func_type.
>>  (output_btf_datasec_types): Update btf_asm_datsec_type call similarly.
>>  (btf_output): Update output_btf_func_types call similarly.
>>
>> gcc/testsuite/
>>
>>  * gcc.dg/debug/btf/btf-array-1.c: Use new BTF asm comments
>>  in scan-assembler expressions where useful.
>>  * gcc.dg/debug/btf/btf-anonymous-struct-1.c: Likewise.
>>  * gcc.dg/debug/btf/btf-anonymous-union-1.c: Likewise.
>>  * gcc.dg/debug/btf/btf-bitfields-2.c: Likewise.
>>  * gcc.dg/debug/btf/btf-bitfields-3.c: Likewise.
>>  * gcc.dg/debug/btf/btf-function-6.c: Likewise.
>>  * gcc.dg/debug/btf/btf-pointers-1.c: Likewise.
>>  * gcc.dg/debug/btf/btf-struct-1.c: Likewise.
>>  * gcc.dg/debug/btf/btf-struct-2.c: Likewise.
>>  * gcc.dg/debug/btf/btf-typedef-1.c: Likewise.
>>  * gcc.dg/debug/btf/btf-union-1.c: Likewise.
>>  * gcc.dg/debug/btf/btf-variables-1.c: Likewise.
>>  * gcc.dg/debug/btf/btf-variables-2.c: Likewise. Update outdated comment.
>>  * gcc.dg/debug/btf/btf-function-3.c: Update outdated comment.
>> ---
>>   gcc/btfout.cc | 220 ++
>>   .../gcc.dg/debug/btf/btf-anonymous-struct-1.c |   3 +-
>>   .../gcc.dg/debug/btf/btf-anonymous-union-1.c  |   4 +-
>>   gcc/testsuite/gcc.dg/debug/btf/btf-array-1.c  |   3 +
>>   .../gcc.dg/debug/btf/btf-bitfields-2.c|   2 +-
>>   .../gcc.dg/debug/btf/btf-bitfields-3.c|   2 +-
>>   .../gcc.dg/debug/btf/btf-function-3.c |   2 +-
>>   .../gcc.dg/debug/btf/btf-function-6.c |   4 +-
>>   .../gcc.dg/debug/btf/btf-pointers-1.c |   3 +
>>   gcc/testsuite/gcc.dg/debug/btf/btf-struct-1.c |   4 +-
>>   gcc/testsuite/gcc.dg/debug/btf/btf-struct-2.c |   2 +-
>>   .../gcc.dg/debug/btf/btf-typedef-1.c  |  14 +-
>>   gcc/testsuite/gcc.dg/debug/btf/btf-union-1.c  |   2 +-
>>   .../gcc.dg/debug/btf/btf-variables-1.c|   6 +
>>   .../gcc.dg/debug/btf/btf-variables-2.c|   7 +-
>>   15 files changed, 215 insertions(+), 63 deletions(-)
>>
>> diff --git a/gcc/btfout.cc b/gcc/btfout.cc
>> index 497c1ca06e6..8960acfbbaa 100644
>> --- a/gcc/btfout.cc
>> +++ b/gcc/btfout.cc
>> @@ -114,6 +114,23 @@ static unsigned int num_types_added = 0;
>>  CTF types.  */
>>   static unsigned int num_types_created = 0;
>>   
>> +/* Name strings for BTF kinds.
>> +   Note: the indices here must match the type defines in btf.h.  */
>> +static const char *const btf_kind_names[] =
>> +  {
>> +"UNKN", 

Re: [PATCH] btf: improve -dA comments for testsuite

2023-05-30 Thread Indu Bhagat via Gcc-patches

On 5/25/23 9:37 AM, David Faust via Gcc-patches wrote:

Many BTF type kinds refer to other types via index to the final types
list. However, the order of the final types list is not guaranteed to
remain the same for the same source program between different runs of
the compiler, making it difficult to test inter-type references.

This patch updates the assembler comments output when writing a
given BTF record to include minimal information about the referenced
type, if any. This allows for the regular expressions used in the gcc
testsuite to do some basic integrity checks on inter-type references.

For example, for the type

unsigned int *

Assembly comments like the following are written with -dA:

.4byte  0   ; TYPE 2 BTF_KIND_PTR ''
.4byte  0x200   ; btt_info: kind=2, kflag=0, vlen=0
.4byte  0x1 ; btt_type: (BTF_KIND_INT 'unsigned int')

Several BTF tests which can immediately be made more robust with this
change are updated. It will also be useful in new tests for the upcoming
btf_type_tag support.



Thanks for working on this, David.  It will be nice to use these 
enhanced assembler comments in the output for some of CTF testing as 
well sometime.  But we can get to that later after this comit.


Some comments inlined below.


Tested on BPF and x86_64, no known regressions.
OK for trunk?

Thanks.

gcc/

* btfout.cc (btf_kind_names): New.
(btf_kind_name): New.
(btf_absolute_var_id): New utility function.
(btf_relative_var_id): Likewise.
(btf_relative_func_id): Likewise.
(btf_absolute_datasec_id): Likewise.
(btf_asm_type_ref): New.
(btf_asm_type): Update asm comments and use btf_asm_type_ref ().
(btf_asm_array): Likewise. Accept ctf_container_ref parameter.
(btf_asm_varent): Likewise.
(btf_asm_func_arg): Likewise.
(btf_asm_datasec_entry): Likewise.
(btf_asm_datasec_type): Likewise.
(btf_asm_func_type): Likewise. Add index parameter.
(btf_asm_sou_member): Likewise.
(output_btf_vars): Update btf_asm_* call accordingly.
(output_asm_btf_sou_fields): Likewise.
(output_asm_btf_func_args_list): Likewise.
(output_asm_btf_vlen_bytes): Likewise.
(output_btf_func_types): Add ctf_container_ref parameter.
Pass it to btf_asm_func_type.
(output_btf_datasec_types): Update btf_asm_datsec_type call similarly.
(btf_output): Update output_btf_func_types call similarly.

gcc/testsuite/

* gcc.dg/debug/btf/btf-array-1.c: Use new BTF asm comments
in scan-assembler expressions where useful.
* gcc.dg/debug/btf/btf-anonymous-struct-1.c: Likewise.
* gcc.dg/debug/btf/btf-anonymous-union-1.c: Likewise.
* gcc.dg/debug/btf/btf-bitfields-2.c: Likewise.
* gcc.dg/debug/btf/btf-bitfields-3.c: Likewise.
* gcc.dg/debug/btf/btf-function-6.c: Likewise.
* gcc.dg/debug/btf/btf-pointers-1.c: Likewise.
* gcc.dg/debug/btf/btf-struct-1.c: Likewise.
* gcc.dg/debug/btf/btf-struct-2.c: Likewise.
* gcc.dg/debug/btf/btf-typedef-1.c: Likewise.
* gcc.dg/debug/btf/btf-union-1.c: Likewise.
* gcc.dg/debug/btf/btf-variables-1.c: Likewise.
* gcc.dg/debug/btf/btf-variables-2.c: Likewise. Update outdated comment.
* gcc.dg/debug/btf/btf-function-3.c: Update outdated comment.
---
  gcc/btfout.cc | 220 ++
  .../gcc.dg/debug/btf/btf-anonymous-struct-1.c |   3 +-
  .../gcc.dg/debug/btf/btf-anonymous-union-1.c  |   4 +-
  gcc/testsuite/gcc.dg/debug/btf/btf-array-1.c  |   3 +
  .../gcc.dg/debug/btf/btf-bitfields-2.c|   2 +-
  .../gcc.dg/debug/btf/btf-bitfields-3.c|   2 +-
  .../gcc.dg/debug/btf/btf-function-3.c |   2 +-
  .../gcc.dg/debug/btf/btf-function-6.c |   4 +-
  .../gcc.dg/debug/btf/btf-pointers-1.c |   3 +
  gcc/testsuite/gcc.dg/debug/btf/btf-struct-1.c |   4 +-
  gcc/testsuite/gcc.dg/debug/btf/btf-struct-2.c |   2 +-
  .../gcc.dg/debug/btf/btf-typedef-1.c  |  14 +-
  gcc/testsuite/gcc.dg/debug/btf/btf-union-1.c  |   2 +-
  .../gcc.dg/debug/btf/btf-variables-1.c|   6 +
  .../gcc.dg/debug/btf/btf-variables-2.c|   7 +-
  15 files changed, 215 insertions(+), 63 deletions(-)

diff --git a/gcc/btfout.cc b/gcc/btfout.cc
index 497c1ca06e6..8960acfbbaa 100644
--- a/gcc/btfout.cc
+++ b/gcc/btfout.cc
@@ -114,6 +114,23 @@ static unsigned int num_types_added = 0;
 CTF types.  */
  static unsigned int num_types_created = 0;
  
+/* Name strings for BTF kinds.

+   Note: the indices here must match the type defines in btf.h.  */
+static const char *const btf_kind_names[] =
+  {
+"UNKN", "INT", "PTR", "ARRAY", "STRUCT", "UNION", "ENUM", "FWD",
+"TYPEDEF", "VOLATILE", "CONST", "RESTRICT", "FUNC", "FUNC_PROTO",
+"VAR", "DATASEC", "FLOAT", "DECL_TAG", "TYPE_TAG", "ENUM64"
+  };
+
+/* Return a name string 

[PATCH] btf: improve -dA comments for testsuite

2023-05-25 Thread David Faust via Gcc-patches
Many BTF type kinds refer to other types via index to the final types
list. However, the order of the final types list is not guaranteed to
remain the same for the same source program between different runs of
the compiler, making it difficult to test inter-type references.

This patch updates the assembler comments output when writing a
given BTF record to include minimal information about the referenced
type, if any. This allows for the regular expressions used in the gcc
testsuite to do some basic integrity checks on inter-type references.

For example, for the type

unsigned int *

Assembly comments like the following are written with -dA:

.4byte  0   ; TYPE 2 BTF_KIND_PTR ''
.4byte  0x200   ; btt_info: kind=2, kflag=0, vlen=0
.4byte  0x1 ; btt_type: (BTF_KIND_INT 'unsigned int')

Several BTF tests which can immediately be made more robust with this
change are updated. It will also be useful in new tests for the upcoming
btf_type_tag support.

Tested on BPF and x86_64, no known regressions.
OK for trunk?

Thanks.

gcc/

* btfout.cc (btf_kind_names): New.
(btf_kind_name): New.
(btf_absolute_var_id): New utility function.
(btf_relative_var_id): Likewise.
(btf_relative_func_id): Likewise.
(btf_absolute_datasec_id): Likewise.
(btf_asm_type_ref): New.
(btf_asm_type): Update asm comments and use btf_asm_type_ref ().
(btf_asm_array): Likewise. Accept ctf_container_ref parameter.
(btf_asm_varent): Likewise.
(btf_asm_func_arg): Likewise.
(btf_asm_datasec_entry): Likewise.
(btf_asm_datasec_type): Likewise.
(btf_asm_func_type): Likewise. Add index parameter.
(btf_asm_sou_member): Likewise.
(output_btf_vars): Update btf_asm_* call accordingly.
(output_asm_btf_sou_fields): Likewise.
(output_asm_btf_func_args_list): Likewise.
(output_asm_btf_vlen_bytes): Likewise.
(output_btf_func_types): Add ctf_container_ref parameter.
Pass it to btf_asm_func_type.
(output_btf_datasec_types): Update btf_asm_datsec_type call similarly.
(btf_output): Update output_btf_func_types call similarly.

gcc/testsuite/

* gcc.dg/debug/btf/btf-array-1.c: Use new BTF asm comments
in scan-assembler expressions where useful.
* gcc.dg/debug/btf/btf-anonymous-struct-1.c: Likewise.
* gcc.dg/debug/btf/btf-anonymous-union-1.c: Likewise.
* gcc.dg/debug/btf/btf-bitfields-2.c: Likewise.
* gcc.dg/debug/btf/btf-bitfields-3.c: Likewise.
* gcc.dg/debug/btf/btf-function-6.c: Likewise.
* gcc.dg/debug/btf/btf-pointers-1.c: Likewise.
* gcc.dg/debug/btf/btf-struct-1.c: Likewise.
* gcc.dg/debug/btf/btf-struct-2.c: Likewise.
* gcc.dg/debug/btf/btf-typedef-1.c: Likewise.
* gcc.dg/debug/btf/btf-union-1.c: Likewise.
* gcc.dg/debug/btf/btf-variables-1.c: Likewise.
* gcc.dg/debug/btf/btf-variables-2.c: Likewise. Update outdated comment.
* gcc.dg/debug/btf/btf-function-3.c: Update outdated comment.
---
 gcc/btfout.cc | 220 ++
 .../gcc.dg/debug/btf/btf-anonymous-struct-1.c |   3 +-
 .../gcc.dg/debug/btf/btf-anonymous-union-1.c  |   4 +-
 gcc/testsuite/gcc.dg/debug/btf/btf-array-1.c  |   3 +
 .../gcc.dg/debug/btf/btf-bitfields-2.c|   2 +-
 .../gcc.dg/debug/btf/btf-bitfields-3.c|   2 +-
 .../gcc.dg/debug/btf/btf-function-3.c |   2 +-
 .../gcc.dg/debug/btf/btf-function-6.c |   4 +-
 .../gcc.dg/debug/btf/btf-pointers-1.c |   3 +
 gcc/testsuite/gcc.dg/debug/btf/btf-struct-1.c |   4 +-
 gcc/testsuite/gcc.dg/debug/btf/btf-struct-2.c |   2 +-
 .../gcc.dg/debug/btf/btf-typedef-1.c  |  14 +-
 gcc/testsuite/gcc.dg/debug/btf/btf-union-1.c  |   2 +-
 .../gcc.dg/debug/btf/btf-variables-1.c|   6 +
 .../gcc.dg/debug/btf/btf-variables-2.c|   7 +-
 15 files changed, 215 insertions(+), 63 deletions(-)

diff --git a/gcc/btfout.cc b/gcc/btfout.cc
index 497c1ca06e6..8960acfbbaa 100644
--- a/gcc/btfout.cc
+++ b/gcc/btfout.cc
@@ -114,6 +114,23 @@ static unsigned int num_types_added = 0;
CTF types.  */
 static unsigned int num_types_created = 0;
 
+/* Name strings for BTF kinds.
+   Note: the indices here must match the type defines in btf.h.  */
+static const char *const btf_kind_names[] =
+  {
+"UNKN", "INT", "PTR", "ARRAY", "STRUCT", "UNION", "ENUM", "FWD",
+"TYPEDEF", "VOLATILE", "CONST", "RESTRICT", "FUNC", "FUNC_PROTO",
+"VAR", "DATASEC", "FLOAT", "DECL_TAG", "TYPE_TAG", "ENUM64"
+  };
+
+/* Return a name string for the given BTF_KIND.  */
+
+static const char *
+btf_kind_name (uint32_t btf_kind)
+{
+  return btf_kind_names[btf_kind];
+}
+
 /* Map a CTF type kind to the corresponding BTF type kind.  */
 
 static uint32_t
@@ -141,6 +158,57 @@ get_btf_kind (uint32_t ctf_kind)
   return BTF_KIND_UNKN;
 }
 
+/* Helper