This patch introduces a new builtin function __builtin_bpf_typeid()
for BPF.
The newly created builtin function issue a constant int64 according
to type feed to it (by utilizing vararg type, __builtin_bpf_typeid
accepts all types of variable), and connect the number with the
specific structure type in Dwarf info by utilizing
EmitTypeAuxAttribute(). The number is computed by simplly
count the number of types which feed to this builtin.
After this patch we have a reliable way to record data and its
type in BPF program. For example, in following program, we output
2 u64 values in different condition.
int func(void *ctx) {
struct {
u64 type;
u64 cycles;
} cycles_vals;
struct {
u64 type;
u64 misses;
} misses_vals;
u32 key = bpf_get_smp_processor_id();
if (key & 1) {
cycles_vals.cycles = bpf_perf_event_read(&map_cycles, key);
cycles_vals.type = __builtin_bpf_typeid(cycles_vals);
bpf_output_trace_data(&cycles_vals, sizeof(cycles_vals));
} else {
misses_vals.misses = bpf_perf_event_read(&map_cache_misses, key);
misses_vals.type = __builtin_bpf_typeid(misses_vals);
bpf_output_trace_data(&misses_vals, sizeof(misses_vals));
}
return 0;
}
And we will get following Dwarf information:
<2><117>: Abbrev Number: 3 (DW_TAG_structure_type)
<118> DW_AT_byte_size : 16
...
<3><11b>: Abbrev Number: 4 (DW_TAG_member)
<11c> DW_AT_name : (indirect string, offset: 0x163): type
...
<126> DW_AT_data_member_location: 0
<3><127>: Abbrev Number: 4 (DW_TAG_member)
<128> DW_AT_name : (indirect string, offset: 0x168): cycles
...
<132> DW_AT_data_member_location: 8
<3><133>: Abbrev Number: 15 (DW_TAG_enumeration_type)
<134> DW_AT_name : (indirect string, offset: 0x173): .llvm.aux.attr
<138> DW_AT_byte_size : 0
<4><139>: Abbrev Number: 16 (DW_TAG_enumerator)
<13a> DW_AT_name : (indirect string, offset: 0x16f): .ID
<13e> DW_AT_const_value : 1
<2><141>: Abbrev Number: 3 (DW_TAG_structure_type)
<142> DW_AT_byte_size : 16
...
<3><145>: Abbrev Number: 4 (DW_TAG_member)
<146> DW_AT_name : (indirect string, offset: 0x163): type
...
<150> DW_AT_data_member_location: 0
<3><151>: Abbrev Number: 4 (DW_TAG_member)
<152> DW_AT_name : (indirect string, offset: 0x182): misses
...
<15c> DW_AT_data_member_location: 8
<3><15d>: Abbrev Number: 15 (DW_TAG_enumeration_type)
<15e> DW_AT_name : (indirect string, offset: 0x173): .llvm.aux.attr
<162> DW_AT_byte_size : 0
<4><163>: Abbrev Number: 16 (DW_TAG_enumerator)
<164> DW_AT_name : (indirect string, offset: 0x16f): .ID
<168> DW_AT_const_value : 2
Then we can connect type id and layout of structure together.
Signed-off-by: Wang Nan <wangnan0@huawei.com>