@@ -12,10 +12,11 @@ static void btf_dump_printf(void *ctx, const char *fmt, va_list args)
12
12
vfprintf (ctx , fmt , args );
13
13
}
14
14
15
- void test_btf_split () {
15
+ static void __test_btf_split (bool multi )
16
+ {
16
17
struct btf_dump * d = NULL ;
17
18
const struct btf_type * t ;
18
- struct btf * btf1 , * btf2 ;
19
+ struct btf * btf1 , * btf2 , * btf3 = NULL ;
19
20
int str_off , i , err ;
20
21
21
22
btf1 = btf__new_empty ();
@@ -63,14 +64,46 @@ void test_btf_split() {
63
64
ASSERT_EQ (btf_vlen (t ), 3 , "split_struct_vlen" );
64
65
ASSERT_STREQ (btf__str_by_offset (btf2 , t -> name_off ), "s2" , "split_struct_name" );
65
66
67
+ if (multi ) {
68
+ btf3 = btf__new_empty_split (btf2 );
69
+ if (!ASSERT_OK_PTR (btf3 , "multi_split_btf" ))
70
+ goto cleanup ;
71
+ } else {
72
+ btf3 = btf2 ;
73
+ }
74
+
75
+ btf__add_union (btf3 , "u1" , 16 ); /* [5] union u1 { */
76
+ btf__add_field (btf3 , "f1" , 4 , 0 , 0 ); /* struct s2 f1; */
77
+ btf__add_field (btf3 , "uf2" , 1 , 0 , 0 ); /* int f2; */
78
+ /* } */
79
+
80
+ if (multi ) {
81
+ t = btf__type_by_id (btf2 , 5 );
82
+ ASSERT_NULL (t , "multisplit_type_in_first_split" );
83
+ }
84
+
85
+ t = btf__type_by_id (btf3 , 5 );
86
+ if (!ASSERT_OK_PTR (t , "split_union_type" ))
87
+ goto cleanup ;
88
+ ASSERT_EQ (btf_is_union (t ), true, "split_union_kind" );
89
+ ASSERT_EQ (btf_vlen (t ), 2 , "split_union_vlen" );
90
+ ASSERT_STREQ (btf__str_by_offset (btf3 , t -> name_off ), "u1" , "split_union_name" );
91
+ ASSERT_EQ (btf__type_cnt (btf3 ), 6 , "split_type_cnt" );
92
+
93
+ t = btf__type_by_id (btf3 , 1 );
94
+ if (!ASSERT_OK_PTR (t , "split_base_type" ))
95
+ goto cleanup ;
96
+ ASSERT_EQ (btf_is_int (t ), true, "split_base_int" );
97
+ ASSERT_STREQ (btf__str_by_offset (btf3 , t -> name_off ), "int" , "split_base_type_name" );
98
+
66
99
/* BTF-to-C dump of split BTF */
67
100
dump_buf_file = open_memstream (& dump_buf , & dump_buf_sz );
68
101
if (!ASSERT_OK_PTR (dump_buf_file , "dump_memstream" ))
69
102
return ;
70
- d = btf_dump__new (btf2 , btf_dump_printf , dump_buf_file , NULL );
103
+ d = btf_dump__new (btf3 , btf_dump_printf , dump_buf_file , NULL );
71
104
if (!ASSERT_OK_PTR (d , "btf_dump__new" ))
72
105
goto cleanup ;
73
- for (i = 1 ; i < btf__type_cnt (btf2 ); i ++ ) {
106
+ for (i = 1 ; i < btf__type_cnt (btf3 ); i ++ ) {
74
107
err = btf_dump__dump_type (d , i );
75
108
ASSERT_OK (err , "dump_type_ok" );
76
109
}
@@ -79,12 +112,15 @@ void test_btf_split() {
79
112
ASSERT_STREQ (dump_buf ,
80
113
"struct s1 {\n"
81
114
" int f1;\n"
82
- "};\n"
83
- "\n"
115
+ "};\n\n"
84
116
"struct s2 {\n"
85
117
" struct s1 f1;\n"
86
118
" int f2;\n"
87
119
" int *f3;\n"
120
+ "};\n\n"
121
+ "union u1 {\n"
122
+ " struct s2 f1;\n"
123
+ " int uf2;\n"
88
124
"};\n\n" , "c_dump" );
89
125
90
126
cleanup :
@@ -94,4 +130,14 @@ void test_btf_split() {
94
130
btf_dump__free (d );
95
131
btf__free (btf1 );
96
132
btf__free (btf2 );
133
+ if (btf2 != btf3 )
134
+ btf__free (btf3 );
135
+ }
136
+
137
+ void test_btf_split (void )
138
+ {
139
+ if (test__start_subtest ("single_split" ))
140
+ __test_btf_split (false);
141
+ if (test__start_subtest ("multi_split" ))
142
+ __test_btf_split (true);
97
143
}
0 commit comments