1*4882a593Smuzhiyun===================== 2*4882a593SmuzhiyunBPF Type Format (BTF) 3*4882a593Smuzhiyun===================== 4*4882a593Smuzhiyun 5*4882a593Smuzhiyun1. Introduction 6*4882a593Smuzhiyun*************** 7*4882a593Smuzhiyun 8*4882a593SmuzhiyunBTF (BPF Type Format) is the metadata format which encodes the debug info 9*4882a593Smuzhiyunrelated to BPF program/map. The name BTF was used initially to describe data 10*4882a593Smuzhiyuntypes. The BTF was later extended to include function info for defined 11*4882a593Smuzhiyunsubroutines, and line info for source/line information. 12*4882a593Smuzhiyun 13*4882a593SmuzhiyunThe debug info is used for map pretty print, function signature, etc. The 14*4882a593Smuzhiyunfunction signature enables better bpf program/function kernel symbol. The line 15*4882a593Smuzhiyuninfo helps generate source annotated translated byte code, jited code and 16*4882a593Smuzhiyunverifier log. 17*4882a593Smuzhiyun 18*4882a593SmuzhiyunThe BTF specification contains two parts, 19*4882a593Smuzhiyun * BTF kernel API 20*4882a593Smuzhiyun * BTF ELF file format 21*4882a593Smuzhiyun 22*4882a593SmuzhiyunThe kernel API is the contract between user space and kernel. The kernel 23*4882a593Smuzhiyunverifies the BTF info before using it. The ELF file format is a user space 24*4882a593Smuzhiyuncontract between ELF file and libbpf loader. 25*4882a593Smuzhiyun 26*4882a593SmuzhiyunThe type and string sections are part of the BTF kernel API, describing the 27*4882a593Smuzhiyundebug info (mostly types related) referenced by the bpf program. These two 28*4882a593Smuzhiyunsections are discussed in details in :ref:`BTF_Type_String`. 29*4882a593Smuzhiyun 30*4882a593Smuzhiyun.. _BTF_Type_String: 31*4882a593Smuzhiyun 32*4882a593Smuzhiyun2. BTF Type and String Encoding 33*4882a593Smuzhiyun******************************* 34*4882a593Smuzhiyun 35*4882a593SmuzhiyunThe file ``include/uapi/linux/btf.h`` provides high-level definition of how 36*4882a593Smuzhiyuntypes/strings are encoded. 37*4882a593Smuzhiyun 38*4882a593SmuzhiyunThe beginning of data blob must be:: 39*4882a593Smuzhiyun 40*4882a593Smuzhiyun struct btf_header { 41*4882a593Smuzhiyun __u16 magic; 42*4882a593Smuzhiyun __u8 version; 43*4882a593Smuzhiyun __u8 flags; 44*4882a593Smuzhiyun __u32 hdr_len; 45*4882a593Smuzhiyun 46*4882a593Smuzhiyun /* All offsets are in bytes relative to the end of this header */ 47*4882a593Smuzhiyun __u32 type_off; /* offset of type section */ 48*4882a593Smuzhiyun __u32 type_len; /* length of type section */ 49*4882a593Smuzhiyun __u32 str_off; /* offset of string section */ 50*4882a593Smuzhiyun __u32 str_len; /* length of string section */ 51*4882a593Smuzhiyun }; 52*4882a593Smuzhiyun 53*4882a593SmuzhiyunThe magic is ``0xeB9F``, which has different encoding for big and little 54*4882a593Smuzhiyunendian systems, and can be used to test whether BTF is generated for big- or 55*4882a593Smuzhiyunlittle-endian target. The ``btf_header`` is designed to be extensible with 56*4882a593Smuzhiyun``hdr_len`` equal to ``sizeof(struct btf_header)`` when a data blob is 57*4882a593Smuzhiyungenerated. 58*4882a593Smuzhiyun 59*4882a593Smuzhiyun2.1 String Encoding 60*4882a593Smuzhiyun=================== 61*4882a593Smuzhiyun 62*4882a593SmuzhiyunThe first string in the string section must be a null string. The rest of 63*4882a593Smuzhiyunstring table is a concatenation of other null-terminated strings. 64*4882a593Smuzhiyun 65*4882a593Smuzhiyun2.2 Type Encoding 66*4882a593Smuzhiyun================= 67*4882a593Smuzhiyun 68*4882a593SmuzhiyunThe type id ``0`` is reserved for ``void`` type. The type section is parsed 69*4882a593Smuzhiyunsequentially and type id is assigned to each recognized type starting from id 70*4882a593Smuzhiyun``1``. Currently, the following types are supported:: 71*4882a593Smuzhiyun 72*4882a593Smuzhiyun #define BTF_KIND_INT 1 /* Integer */ 73*4882a593Smuzhiyun #define BTF_KIND_PTR 2 /* Pointer */ 74*4882a593Smuzhiyun #define BTF_KIND_ARRAY 3 /* Array */ 75*4882a593Smuzhiyun #define BTF_KIND_STRUCT 4 /* Struct */ 76*4882a593Smuzhiyun #define BTF_KIND_UNION 5 /* Union */ 77*4882a593Smuzhiyun #define BTF_KIND_ENUM 6 /* Enumeration */ 78*4882a593Smuzhiyun #define BTF_KIND_FWD 7 /* Forward */ 79*4882a593Smuzhiyun #define BTF_KIND_TYPEDEF 8 /* Typedef */ 80*4882a593Smuzhiyun #define BTF_KIND_VOLATILE 9 /* Volatile */ 81*4882a593Smuzhiyun #define BTF_KIND_CONST 10 /* Const */ 82*4882a593Smuzhiyun #define BTF_KIND_RESTRICT 11 /* Restrict */ 83*4882a593Smuzhiyun #define BTF_KIND_FUNC 12 /* Function */ 84*4882a593Smuzhiyun #define BTF_KIND_FUNC_PROTO 13 /* Function Proto */ 85*4882a593Smuzhiyun #define BTF_KIND_VAR 14 /* Variable */ 86*4882a593Smuzhiyun #define BTF_KIND_DATASEC 15 /* Section */ 87*4882a593Smuzhiyun 88*4882a593SmuzhiyunNote that the type section encodes debug info, not just pure types. 89*4882a593Smuzhiyun``BTF_KIND_FUNC`` is not a type, and it represents a defined subprogram. 90*4882a593Smuzhiyun 91*4882a593SmuzhiyunEach type contains the following common data:: 92*4882a593Smuzhiyun 93*4882a593Smuzhiyun struct btf_type { 94*4882a593Smuzhiyun __u32 name_off; 95*4882a593Smuzhiyun /* "info" bits arrangement 96*4882a593Smuzhiyun * bits 0-15: vlen (e.g. # of struct's members) 97*4882a593Smuzhiyun * bits 16-23: unused 98*4882a593Smuzhiyun * bits 24-27: kind (e.g. int, ptr, array...etc) 99*4882a593Smuzhiyun * bits 28-30: unused 100*4882a593Smuzhiyun * bit 31: kind_flag, currently used by 101*4882a593Smuzhiyun * struct, union and fwd 102*4882a593Smuzhiyun */ 103*4882a593Smuzhiyun __u32 info; 104*4882a593Smuzhiyun /* "size" is used by INT, ENUM, STRUCT and UNION. 105*4882a593Smuzhiyun * "size" tells the size of the type it is describing. 106*4882a593Smuzhiyun * 107*4882a593Smuzhiyun * "type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT, 108*4882a593Smuzhiyun * FUNC and FUNC_PROTO. 109*4882a593Smuzhiyun * "type" is a type_id referring to another type. 110*4882a593Smuzhiyun */ 111*4882a593Smuzhiyun union { 112*4882a593Smuzhiyun __u32 size; 113*4882a593Smuzhiyun __u32 type; 114*4882a593Smuzhiyun }; 115*4882a593Smuzhiyun }; 116*4882a593Smuzhiyun 117*4882a593SmuzhiyunFor certain kinds, the common data are followed by kind-specific data. The 118*4882a593Smuzhiyun``name_off`` in ``struct btf_type`` specifies the offset in the string table. 119*4882a593SmuzhiyunThe following sections detail encoding of each kind. 120*4882a593Smuzhiyun 121*4882a593Smuzhiyun2.2.1 BTF_KIND_INT 122*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~ 123*4882a593Smuzhiyun 124*4882a593Smuzhiyun``struct btf_type`` encoding requirement: 125*4882a593Smuzhiyun * ``name_off``: any valid offset 126*4882a593Smuzhiyun * ``info.kind_flag``: 0 127*4882a593Smuzhiyun * ``info.kind``: BTF_KIND_INT 128*4882a593Smuzhiyun * ``info.vlen``: 0 129*4882a593Smuzhiyun * ``size``: the size of the int type in bytes. 130*4882a593Smuzhiyun 131*4882a593Smuzhiyun``btf_type`` is followed by a ``u32`` with the following bits arrangement:: 132*4882a593Smuzhiyun 133*4882a593Smuzhiyun #define BTF_INT_ENCODING(VAL) (((VAL) & 0x0f000000) >> 24) 134*4882a593Smuzhiyun #define BTF_INT_OFFSET(VAL) (((VAL) & 0x00ff0000) >> 16) 135*4882a593Smuzhiyun #define BTF_INT_BITS(VAL) ((VAL) & 0x000000ff) 136*4882a593Smuzhiyun 137*4882a593SmuzhiyunThe ``BTF_INT_ENCODING`` has the following attributes:: 138*4882a593Smuzhiyun 139*4882a593Smuzhiyun #define BTF_INT_SIGNED (1 << 0) 140*4882a593Smuzhiyun #define BTF_INT_CHAR (1 << 1) 141*4882a593Smuzhiyun #define BTF_INT_BOOL (1 << 2) 142*4882a593Smuzhiyun 143*4882a593SmuzhiyunThe ``BTF_INT_ENCODING()`` provides extra information: signedness, char, or 144*4882a593Smuzhiyunbool, for the int type. The char and bool encoding are mostly useful for 145*4882a593Smuzhiyunpretty print. At most one encoding can be specified for the int type. 146*4882a593Smuzhiyun 147*4882a593SmuzhiyunThe ``BTF_INT_BITS()`` specifies the number of actual bits held by this int 148*4882a593Smuzhiyuntype. For example, a 4-bit bitfield encodes ``BTF_INT_BITS()`` equals to 4. 149*4882a593SmuzhiyunThe ``btf_type.size * 8`` must be equal to or greater than ``BTF_INT_BITS()`` 150*4882a593Smuzhiyunfor the type. The maximum value of ``BTF_INT_BITS()`` is 128. 151*4882a593Smuzhiyun 152*4882a593SmuzhiyunThe ``BTF_INT_OFFSET()`` specifies the starting bit offset to calculate values 153*4882a593Smuzhiyunfor this int. For example, a bitfield struct member has: 154*4882a593Smuzhiyun 155*4882a593Smuzhiyun * btf member bit offset 100 from the start of the structure, 156*4882a593Smuzhiyun * btf member pointing to an int type, 157*4882a593Smuzhiyun * the int type has ``BTF_INT_OFFSET() = 2`` and ``BTF_INT_BITS() = 4`` 158*4882a593Smuzhiyun 159*4882a593SmuzhiyunThen in the struct memory layout, this member will occupy ``4`` bits starting 160*4882a593Smuzhiyunfrom bits ``100 + 2 = 102``. 161*4882a593Smuzhiyun 162*4882a593SmuzhiyunAlternatively, the bitfield struct member can be the following to access the 163*4882a593Smuzhiyunsame bits as the above: 164*4882a593Smuzhiyun 165*4882a593Smuzhiyun * btf member bit offset 102, 166*4882a593Smuzhiyun * btf member pointing to an int type, 167*4882a593Smuzhiyun * the int type has ``BTF_INT_OFFSET() = 0`` and ``BTF_INT_BITS() = 4`` 168*4882a593Smuzhiyun 169*4882a593SmuzhiyunThe original intention of ``BTF_INT_OFFSET()`` is to provide flexibility of 170*4882a593Smuzhiyunbitfield encoding. Currently, both llvm and pahole generate 171*4882a593Smuzhiyun``BTF_INT_OFFSET() = 0`` for all int types. 172*4882a593Smuzhiyun 173*4882a593Smuzhiyun2.2.2 BTF_KIND_PTR 174*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~ 175*4882a593Smuzhiyun 176*4882a593Smuzhiyun``struct btf_type`` encoding requirement: 177*4882a593Smuzhiyun * ``name_off``: 0 178*4882a593Smuzhiyun * ``info.kind_flag``: 0 179*4882a593Smuzhiyun * ``info.kind``: BTF_KIND_PTR 180*4882a593Smuzhiyun * ``info.vlen``: 0 181*4882a593Smuzhiyun * ``type``: the pointee type of the pointer 182*4882a593Smuzhiyun 183*4882a593SmuzhiyunNo additional type data follow ``btf_type``. 184*4882a593Smuzhiyun 185*4882a593Smuzhiyun2.2.3 BTF_KIND_ARRAY 186*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~ 187*4882a593Smuzhiyun 188*4882a593Smuzhiyun``struct btf_type`` encoding requirement: 189*4882a593Smuzhiyun * ``name_off``: 0 190*4882a593Smuzhiyun * ``info.kind_flag``: 0 191*4882a593Smuzhiyun * ``info.kind``: BTF_KIND_ARRAY 192*4882a593Smuzhiyun * ``info.vlen``: 0 193*4882a593Smuzhiyun * ``size/type``: 0, not used 194*4882a593Smuzhiyun 195*4882a593Smuzhiyun``btf_type`` is followed by one ``struct btf_array``:: 196*4882a593Smuzhiyun 197*4882a593Smuzhiyun struct btf_array { 198*4882a593Smuzhiyun __u32 type; 199*4882a593Smuzhiyun __u32 index_type; 200*4882a593Smuzhiyun __u32 nelems; 201*4882a593Smuzhiyun }; 202*4882a593Smuzhiyun 203*4882a593SmuzhiyunThe ``struct btf_array`` encoding: 204*4882a593Smuzhiyun * ``type``: the element type 205*4882a593Smuzhiyun * ``index_type``: the index type 206*4882a593Smuzhiyun * ``nelems``: the number of elements for this array (``0`` is also allowed). 207*4882a593Smuzhiyun 208*4882a593SmuzhiyunThe ``index_type`` can be any regular int type (``u8``, ``u16``, ``u32``, 209*4882a593Smuzhiyun``u64``, ``unsigned __int128``). The original design of including 210*4882a593Smuzhiyun``index_type`` follows DWARF, which has an ``index_type`` for its array type. 211*4882a593SmuzhiyunCurrently in BTF, beyond type verification, the ``index_type`` is not used. 212*4882a593Smuzhiyun 213*4882a593SmuzhiyunThe ``struct btf_array`` allows chaining through element type to represent 214*4882a593Smuzhiyunmultidimensional arrays. For example, for ``int a[5][6]``, the following type 215*4882a593Smuzhiyuninformation illustrates the chaining: 216*4882a593Smuzhiyun 217*4882a593Smuzhiyun * [1]: int 218*4882a593Smuzhiyun * [2]: array, ``btf_array.type = [1]``, ``btf_array.nelems = 6`` 219*4882a593Smuzhiyun * [3]: array, ``btf_array.type = [2]``, ``btf_array.nelems = 5`` 220*4882a593Smuzhiyun 221*4882a593SmuzhiyunCurrently, both pahole and llvm collapse multidimensional array into 222*4882a593Smuzhiyunone-dimensional array, e.g., for ``a[5][6]``, the ``btf_array.nelems`` is 223*4882a593Smuzhiyunequal to ``30``. This is because the original use case is map pretty print 224*4882a593Smuzhiyunwhere the whole array is dumped out so one-dimensional array is enough. As 225*4882a593Smuzhiyunmore BTF usage is explored, pahole and llvm can be changed to generate proper 226*4882a593Smuzhiyunchained representation for multidimensional arrays. 227*4882a593Smuzhiyun 228*4882a593Smuzhiyun2.2.4 BTF_KIND_STRUCT 229*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~ 230*4882a593Smuzhiyun2.2.5 BTF_KIND_UNION 231*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~ 232*4882a593Smuzhiyun 233*4882a593Smuzhiyun``struct btf_type`` encoding requirement: 234*4882a593Smuzhiyun * ``name_off``: 0 or offset to a valid C identifier 235*4882a593Smuzhiyun * ``info.kind_flag``: 0 or 1 236*4882a593Smuzhiyun * ``info.kind``: BTF_KIND_STRUCT or BTF_KIND_UNION 237*4882a593Smuzhiyun * ``info.vlen``: the number of struct/union members 238*4882a593Smuzhiyun * ``info.size``: the size of the struct/union in bytes 239*4882a593Smuzhiyun 240*4882a593Smuzhiyun``btf_type`` is followed by ``info.vlen`` number of ``struct btf_member``.:: 241*4882a593Smuzhiyun 242*4882a593Smuzhiyun struct btf_member { 243*4882a593Smuzhiyun __u32 name_off; 244*4882a593Smuzhiyun __u32 type; 245*4882a593Smuzhiyun __u32 offset; 246*4882a593Smuzhiyun }; 247*4882a593Smuzhiyun 248*4882a593Smuzhiyun``struct btf_member`` encoding: 249*4882a593Smuzhiyun * ``name_off``: offset to a valid C identifier 250*4882a593Smuzhiyun * ``type``: the member type 251*4882a593Smuzhiyun * ``offset``: <see below> 252*4882a593Smuzhiyun 253*4882a593SmuzhiyunIf the type info ``kind_flag`` is not set, the offset contains only bit offset 254*4882a593Smuzhiyunof the member. Note that the base type of the bitfield can only be int or enum 255*4882a593Smuzhiyuntype. If the bitfield size is 32, the base type can be either int or enum 256*4882a593Smuzhiyuntype. If the bitfield size is not 32, the base type must be int, and int type 257*4882a593Smuzhiyun``BTF_INT_BITS()`` encodes the bitfield size. 258*4882a593Smuzhiyun 259*4882a593SmuzhiyunIf the ``kind_flag`` is set, the ``btf_member.offset`` contains both member 260*4882a593Smuzhiyunbitfield size and bit offset. The bitfield size and bit offset are calculated 261*4882a593Smuzhiyunas below.:: 262*4882a593Smuzhiyun 263*4882a593Smuzhiyun #define BTF_MEMBER_BITFIELD_SIZE(val) ((val) >> 24) 264*4882a593Smuzhiyun #define BTF_MEMBER_BIT_OFFSET(val) ((val) & 0xffffff) 265*4882a593Smuzhiyun 266*4882a593SmuzhiyunIn this case, if the base type is an int type, it must be a regular int type: 267*4882a593Smuzhiyun 268*4882a593Smuzhiyun * ``BTF_INT_OFFSET()`` must be 0. 269*4882a593Smuzhiyun * ``BTF_INT_BITS()`` must be equal to ``{1,2,4,8,16} * 8``. 270*4882a593Smuzhiyun 271*4882a593SmuzhiyunThe following kernel patch introduced ``kind_flag`` and explained why both 272*4882a593Smuzhiyunmodes exist: 273*4882a593Smuzhiyun 274*4882a593Smuzhiyun https://github.com/torvalds/linux/commit/9d5f9f701b1891466fb3dbb1806ad97716f95cc3#diff-fa650a64fdd3968396883d2fe8215ff3 275*4882a593Smuzhiyun 276*4882a593Smuzhiyun2.2.6 BTF_KIND_ENUM 277*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~ 278*4882a593Smuzhiyun 279*4882a593Smuzhiyun``struct btf_type`` encoding requirement: 280*4882a593Smuzhiyun * ``name_off``: 0 or offset to a valid C identifier 281*4882a593Smuzhiyun * ``info.kind_flag``: 0 282*4882a593Smuzhiyun * ``info.kind``: BTF_KIND_ENUM 283*4882a593Smuzhiyun * ``info.vlen``: number of enum values 284*4882a593Smuzhiyun * ``size``: 4 285*4882a593Smuzhiyun 286*4882a593Smuzhiyun``btf_type`` is followed by ``info.vlen`` number of ``struct btf_enum``.:: 287*4882a593Smuzhiyun 288*4882a593Smuzhiyun struct btf_enum { 289*4882a593Smuzhiyun __u32 name_off; 290*4882a593Smuzhiyun __s32 val; 291*4882a593Smuzhiyun }; 292*4882a593Smuzhiyun 293*4882a593SmuzhiyunThe ``btf_enum`` encoding: 294*4882a593Smuzhiyun * ``name_off``: offset to a valid C identifier 295*4882a593Smuzhiyun * ``val``: any value 296*4882a593Smuzhiyun 297*4882a593Smuzhiyun2.2.7 BTF_KIND_FWD 298*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~ 299*4882a593Smuzhiyun 300*4882a593Smuzhiyun``struct btf_type`` encoding requirement: 301*4882a593Smuzhiyun * ``name_off``: offset to a valid C identifier 302*4882a593Smuzhiyun * ``info.kind_flag``: 0 for struct, 1 for union 303*4882a593Smuzhiyun * ``info.kind``: BTF_KIND_FWD 304*4882a593Smuzhiyun * ``info.vlen``: 0 305*4882a593Smuzhiyun * ``type``: 0 306*4882a593Smuzhiyun 307*4882a593SmuzhiyunNo additional type data follow ``btf_type``. 308*4882a593Smuzhiyun 309*4882a593Smuzhiyun2.2.8 BTF_KIND_TYPEDEF 310*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~ 311*4882a593Smuzhiyun 312*4882a593Smuzhiyun``struct btf_type`` encoding requirement: 313*4882a593Smuzhiyun * ``name_off``: offset to a valid C identifier 314*4882a593Smuzhiyun * ``info.kind_flag``: 0 315*4882a593Smuzhiyun * ``info.kind``: BTF_KIND_TYPEDEF 316*4882a593Smuzhiyun * ``info.vlen``: 0 317*4882a593Smuzhiyun * ``type``: the type which can be referred by name at ``name_off`` 318*4882a593Smuzhiyun 319*4882a593SmuzhiyunNo additional type data follow ``btf_type``. 320*4882a593Smuzhiyun 321*4882a593Smuzhiyun2.2.9 BTF_KIND_VOLATILE 322*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~ 323*4882a593Smuzhiyun 324*4882a593Smuzhiyun``struct btf_type`` encoding requirement: 325*4882a593Smuzhiyun * ``name_off``: 0 326*4882a593Smuzhiyun * ``info.kind_flag``: 0 327*4882a593Smuzhiyun * ``info.kind``: BTF_KIND_VOLATILE 328*4882a593Smuzhiyun * ``info.vlen``: 0 329*4882a593Smuzhiyun * ``type``: the type with ``volatile`` qualifier 330*4882a593Smuzhiyun 331*4882a593SmuzhiyunNo additional type data follow ``btf_type``. 332*4882a593Smuzhiyun 333*4882a593Smuzhiyun2.2.10 BTF_KIND_CONST 334*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~ 335*4882a593Smuzhiyun 336*4882a593Smuzhiyun``struct btf_type`` encoding requirement: 337*4882a593Smuzhiyun * ``name_off``: 0 338*4882a593Smuzhiyun * ``info.kind_flag``: 0 339*4882a593Smuzhiyun * ``info.kind``: BTF_KIND_CONST 340*4882a593Smuzhiyun * ``info.vlen``: 0 341*4882a593Smuzhiyun * ``type``: the type with ``const`` qualifier 342*4882a593Smuzhiyun 343*4882a593SmuzhiyunNo additional type data follow ``btf_type``. 344*4882a593Smuzhiyun 345*4882a593Smuzhiyun2.2.11 BTF_KIND_RESTRICT 346*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~ 347*4882a593Smuzhiyun 348*4882a593Smuzhiyun``struct btf_type`` encoding requirement: 349*4882a593Smuzhiyun * ``name_off``: 0 350*4882a593Smuzhiyun * ``info.kind_flag``: 0 351*4882a593Smuzhiyun * ``info.kind``: BTF_KIND_RESTRICT 352*4882a593Smuzhiyun * ``info.vlen``: 0 353*4882a593Smuzhiyun * ``type``: the type with ``restrict`` qualifier 354*4882a593Smuzhiyun 355*4882a593SmuzhiyunNo additional type data follow ``btf_type``. 356*4882a593Smuzhiyun 357*4882a593Smuzhiyun2.2.12 BTF_KIND_FUNC 358*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~ 359*4882a593Smuzhiyun 360*4882a593Smuzhiyun``struct btf_type`` encoding requirement: 361*4882a593Smuzhiyun * ``name_off``: offset to a valid C identifier 362*4882a593Smuzhiyun * ``info.kind_flag``: 0 363*4882a593Smuzhiyun * ``info.kind``: BTF_KIND_FUNC 364*4882a593Smuzhiyun * ``info.vlen``: 0 365*4882a593Smuzhiyun * ``type``: a BTF_KIND_FUNC_PROTO type 366*4882a593Smuzhiyun 367*4882a593SmuzhiyunNo additional type data follow ``btf_type``. 368*4882a593Smuzhiyun 369*4882a593SmuzhiyunA BTF_KIND_FUNC defines not a type, but a subprogram (function) whose 370*4882a593Smuzhiyunsignature is defined by ``type``. The subprogram is thus an instance of that 371*4882a593Smuzhiyuntype. The BTF_KIND_FUNC may in turn be referenced by a func_info in the 372*4882a593Smuzhiyun:ref:`BTF_Ext_Section` (ELF) or in the arguments to :ref:`BPF_Prog_Load` 373*4882a593Smuzhiyun(ABI). 374*4882a593Smuzhiyun 375*4882a593Smuzhiyun2.2.13 BTF_KIND_FUNC_PROTO 376*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~ 377*4882a593Smuzhiyun 378*4882a593Smuzhiyun``struct btf_type`` encoding requirement: 379*4882a593Smuzhiyun * ``name_off``: 0 380*4882a593Smuzhiyun * ``info.kind_flag``: 0 381*4882a593Smuzhiyun * ``info.kind``: BTF_KIND_FUNC_PROTO 382*4882a593Smuzhiyun * ``info.vlen``: # of parameters 383*4882a593Smuzhiyun * ``type``: the return type 384*4882a593Smuzhiyun 385*4882a593Smuzhiyun``btf_type`` is followed by ``info.vlen`` number of ``struct btf_param``.:: 386*4882a593Smuzhiyun 387*4882a593Smuzhiyun struct btf_param { 388*4882a593Smuzhiyun __u32 name_off; 389*4882a593Smuzhiyun __u32 type; 390*4882a593Smuzhiyun }; 391*4882a593Smuzhiyun 392*4882a593SmuzhiyunIf a BTF_KIND_FUNC_PROTO type is referred by a BTF_KIND_FUNC type, then 393*4882a593Smuzhiyun``btf_param.name_off`` must point to a valid C identifier except for the 394*4882a593Smuzhiyunpossible last argument representing the variable argument. The btf_param.type 395*4882a593Smuzhiyunrefers to parameter type. 396*4882a593Smuzhiyun 397*4882a593SmuzhiyunIf the function has variable arguments, the last parameter is encoded with 398*4882a593Smuzhiyun``name_off = 0`` and ``type = 0``. 399*4882a593Smuzhiyun 400*4882a593Smuzhiyun2.2.14 BTF_KIND_VAR 401*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~ 402*4882a593Smuzhiyun 403*4882a593Smuzhiyun``struct btf_type`` encoding requirement: 404*4882a593Smuzhiyun * ``name_off``: offset to a valid C identifier 405*4882a593Smuzhiyun * ``info.kind_flag``: 0 406*4882a593Smuzhiyun * ``info.kind``: BTF_KIND_VAR 407*4882a593Smuzhiyun * ``info.vlen``: 0 408*4882a593Smuzhiyun * ``type``: the type of the variable 409*4882a593Smuzhiyun 410*4882a593Smuzhiyun``btf_type`` is followed by a single ``struct btf_variable`` with the 411*4882a593Smuzhiyunfollowing data:: 412*4882a593Smuzhiyun 413*4882a593Smuzhiyun struct btf_var { 414*4882a593Smuzhiyun __u32 linkage; 415*4882a593Smuzhiyun }; 416*4882a593Smuzhiyun 417*4882a593Smuzhiyun``struct btf_var`` encoding: 418*4882a593Smuzhiyun * ``linkage``: currently only static variable 0, or globally allocated 419*4882a593Smuzhiyun variable in ELF sections 1 420*4882a593Smuzhiyun 421*4882a593SmuzhiyunNot all type of global variables are supported by LLVM at this point. 422*4882a593SmuzhiyunThe following is currently available: 423*4882a593Smuzhiyun 424*4882a593Smuzhiyun * static variables with or without section attributes 425*4882a593Smuzhiyun * global variables with section attributes 426*4882a593Smuzhiyun 427*4882a593SmuzhiyunThe latter is for future extraction of map key/value type id's from a 428*4882a593Smuzhiyunmap definition. 429*4882a593Smuzhiyun 430*4882a593Smuzhiyun2.2.15 BTF_KIND_DATASEC 431*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~ 432*4882a593Smuzhiyun 433*4882a593Smuzhiyun``struct btf_type`` encoding requirement: 434*4882a593Smuzhiyun * ``name_off``: offset to a valid name associated with a variable or 435*4882a593Smuzhiyun one of .data/.bss/.rodata 436*4882a593Smuzhiyun * ``info.kind_flag``: 0 437*4882a593Smuzhiyun * ``info.kind``: BTF_KIND_DATASEC 438*4882a593Smuzhiyun * ``info.vlen``: # of variables 439*4882a593Smuzhiyun * ``size``: total section size in bytes (0 at compilation time, patched 440*4882a593Smuzhiyun to actual size by BPF loaders such as libbpf) 441*4882a593Smuzhiyun 442*4882a593Smuzhiyun``btf_type`` is followed by ``info.vlen`` number of ``struct btf_var_secinfo``.:: 443*4882a593Smuzhiyun 444*4882a593Smuzhiyun struct btf_var_secinfo { 445*4882a593Smuzhiyun __u32 type; 446*4882a593Smuzhiyun __u32 offset; 447*4882a593Smuzhiyun __u32 size; 448*4882a593Smuzhiyun }; 449*4882a593Smuzhiyun 450*4882a593Smuzhiyun``struct btf_var_secinfo`` encoding: 451*4882a593Smuzhiyun * ``type``: the type of the BTF_KIND_VAR variable 452*4882a593Smuzhiyun * ``offset``: the in-section offset of the variable 453*4882a593Smuzhiyun * ``size``: the size of the variable in bytes 454*4882a593Smuzhiyun 455*4882a593Smuzhiyun3. BTF Kernel API 456*4882a593Smuzhiyun***************** 457*4882a593Smuzhiyun 458*4882a593SmuzhiyunThe following bpf syscall command involves BTF: 459*4882a593Smuzhiyun * BPF_BTF_LOAD: load a blob of BTF data into kernel 460*4882a593Smuzhiyun * BPF_MAP_CREATE: map creation with btf key and value type info. 461*4882a593Smuzhiyun * BPF_PROG_LOAD: prog load with btf function and line info. 462*4882a593Smuzhiyun * BPF_BTF_GET_FD_BY_ID: get a btf fd 463*4882a593Smuzhiyun * BPF_OBJ_GET_INFO_BY_FD: btf, func_info, line_info 464*4882a593Smuzhiyun and other btf related info are returned. 465*4882a593Smuzhiyun 466*4882a593SmuzhiyunThe workflow typically looks like: 467*4882a593Smuzhiyun:: 468*4882a593Smuzhiyun 469*4882a593Smuzhiyun Application: 470*4882a593Smuzhiyun BPF_BTF_LOAD 471*4882a593Smuzhiyun | 472*4882a593Smuzhiyun v 473*4882a593Smuzhiyun BPF_MAP_CREATE and BPF_PROG_LOAD 474*4882a593Smuzhiyun | 475*4882a593Smuzhiyun V 476*4882a593Smuzhiyun ...... 477*4882a593Smuzhiyun 478*4882a593Smuzhiyun Introspection tool: 479*4882a593Smuzhiyun ...... 480*4882a593Smuzhiyun BPF_{PROG,MAP}_GET_NEXT_ID (get prog/map id's) 481*4882a593Smuzhiyun | 482*4882a593Smuzhiyun V 483*4882a593Smuzhiyun BPF_{PROG,MAP}_GET_FD_BY_ID (get a prog/map fd) 484*4882a593Smuzhiyun | 485*4882a593Smuzhiyun V 486*4882a593Smuzhiyun BPF_OBJ_GET_INFO_BY_FD (get bpf_prog_info/bpf_map_info with btf_id) 487*4882a593Smuzhiyun | | 488*4882a593Smuzhiyun V | 489*4882a593Smuzhiyun BPF_BTF_GET_FD_BY_ID (get btf_fd) | 490*4882a593Smuzhiyun | | 491*4882a593Smuzhiyun V | 492*4882a593Smuzhiyun BPF_OBJ_GET_INFO_BY_FD (get btf) | 493*4882a593Smuzhiyun | | 494*4882a593Smuzhiyun V V 495*4882a593Smuzhiyun pretty print types, dump func signatures and line info, etc. 496*4882a593Smuzhiyun 497*4882a593Smuzhiyun 498*4882a593Smuzhiyun3.1 BPF_BTF_LOAD 499*4882a593Smuzhiyun================ 500*4882a593Smuzhiyun 501*4882a593SmuzhiyunLoad a blob of BTF data into kernel. A blob of data, described in 502*4882a593Smuzhiyun:ref:`BTF_Type_String`, can be directly loaded into the kernel. A ``btf_fd`` 503*4882a593Smuzhiyunis returned to a userspace. 504*4882a593Smuzhiyun 505*4882a593Smuzhiyun3.2 BPF_MAP_CREATE 506*4882a593Smuzhiyun================== 507*4882a593Smuzhiyun 508*4882a593SmuzhiyunA map can be created with ``btf_fd`` and specified key/value type id.:: 509*4882a593Smuzhiyun 510*4882a593Smuzhiyun __u32 btf_fd; /* fd pointing to a BTF type data */ 511*4882a593Smuzhiyun __u32 btf_key_type_id; /* BTF type_id of the key */ 512*4882a593Smuzhiyun __u32 btf_value_type_id; /* BTF type_id of the value */ 513*4882a593Smuzhiyun 514*4882a593SmuzhiyunIn libbpf, the map can be defined with extra annotation like below: 515*4882a593Smuzhiyun:: 516*4882a593Smuzhiyun 517*4882a593Smuzhiyun struct bpf_map_def SEC("maps") btf_map = { 518*4882a593Smuzhiyun .type = BPF_MAP_TYPE_ARRAY, 519*4882a593Smuzhiyun .key_size = sizeof(int), 520*4882a593Smuzhiyun .value_size = sizeof(struct ipv_counts), 521*4882a593Smuzhiyun .max_entries = 4, 522*4882a593Smuzhiyun }; 523*4882a593Smuzhiyun BPF_ANNOTATE_KV_PAIR(btf_map, int, struct ipv_counts); 524*4882a593Smuzhiyun 525*4882a593SmuzhiyunHere, the parameters for macro BPF_ANNOTATE_KV_PAIR are map name, key and 526*4882a593Smuzhiyunvalue types for the map. During ELF parsing, libbpf is able to extract 527*4882a593Smuzhiyunkey/value type_id's and assign them to BPF_MAP_CREATE attributes 528*4882a593Smuzhiyunautomatically. 529*4882a593Smuzhiyun 530*4882a593Smuzhiyun.. _BPF_Prog_Load: 531*4882a593Smuzhiyun 532*4882a593Smuzhiyun3.3 BPF_PROG_LOAD 533*4882a593Smuzhiyun================= 534*4882a593Smuzhiyun 535*4882a593SmuzhiyunDuring prog_load, func_info and line_info can be passed to kernel with proper 536*4882a593Smuzhiyunvalues for the following attributes: 537*4882a593Smuzhiyun:: 538*4882a593Smuzhiyun 539*4882a593Smuzhiyun __u32 insn_cnt; 540*4882a593Smuzhiyun __aligned_u64 insns; 541*4882a593Smuzhiyun ...... 542*4882a593Smuzhiyun __u32 prog_btf_fd; /* fd pointing to BTF type data */ 543*4882a593Smuzhiyun __u32 func_info_rec_size; /* userspace bpf_func_info size */ 544*4882a593Smuzhiyun __aligned_u64 func_info; /* func info */ 545*4882a593Smuzhiyun __u32 func_info_cnt; /* number of bpf_func_info records */ 546*4882a593Smuzhiyun __u32 line_info_rec_size; /* userspace bpf_line_info size */ 547*4882a593Smuzhiyun __aligned_u64 line_info; /* line info */ 548*4882a593Smuzhiyun __u32 line_info_cnt; /* number of bpf_line_info records */ 549*4882a593Smuzhiyun 550*4882a593SmuzhiyunThe func_info and line_info are an array of below, respectively.:: 551*4882a593Smuzhiyun 552*4882a593Smuzhiyun struct bpf_func_info { 553*4882a593Smuzhiyun __u32 insn_off; /* [0, insn_cnt - 1] */ 554*4882a593Smuzhiyun __u32 type_id; /* pointing to a BTF_KIND_FUNC type */ 555*4882a593Smuzhiyun }; 556*4882a593Smuzhiyun struct bpf_line_info { 557*4882a593Smuzhiyun __u32 insn_off; /* [0, insn_cnt - 1] */ 558*4882a593Smuzhiyun __u32 file_name_off; /* offset to string table for the filename */ 559*4882a593Smuzhiyun __u32 line_off; /* offset to string table for the source line */ 560*4882a593Smuzhiyun __u32 line_col; /* line number and column number */ 561*4882a593Smuzhiyun }; 562*4882a593Smuzhiyun 563*4882a593Smuzhiyunfunc_info_rec_size is the size of each func_info record, and 564*4882a593Smuzhiyunline_info_rec_size is the size of each line_info record. Passing the record 565*4882a593Smuzhiyunsize to kernel make it possible to extend the record itself in the future. 566*4882a593Smuzhiyun 567*4882a593SmuzhiyunBelow are requirements for func_info: 568*4882a593Smuzhiyun * func_info[0].insn_off must be 0. 569*4882a593Smuzhiyun * the func_info insn_off is in strictly increasing order and matches 570*4882a593Smuzhiyun bpf func boundaries. 571*4882a593Smuzhiyun 572*4882a593SmuzhiyunBelow are requirements for line_info: 573*4882a593Smuzhiyun * the first insn in each func must have a line_info record pointing to it. 574*4882a593Smuzhiyun * the line_info insn_off is in strictly increasing order. 575*4882a593Smuzhiyun 576*4882a593SmuzhiyunFor line_info, the line number and column number are defined as below: 577*4882a593Smuzhiyun:: 578*4882a593Smuzhiyun 579*4882a593Smuzhiyun #define BPF_LINE_INFO_LINE_NUM(line_col) ((line_col) >> 10) 580*4882a593Smuzhiyun #define BPF_LINE_INFO_LINE_COL(line_col) ((line_col) & 0x3ff) 581*4882a593Smuzhiyun 582*4882a593Smuzhiyun3.4 BPF_{PROG,MAP}_GET_NEXT_ID 583*4882a593Smuzhiyun============================== 584*4882a593Smuzhiyun 585*4882a593SmuzhiyunIn kernel, every loaded program, map or btf has a unique id. The id won't 586*4882a593Smuzhiyunchange during the lifetime of a program, map, or btf. 587*4882a593Smuzhiyun 588*4882a593SmuzhiyunThe bpf syscall command BPF_{PROG,MAP}_GET_NEXT_ID returns all id's, one for 589*4882a593Smuzhiyuneach command, to user space, for bpf program or maps, respectively, so an 590*4882a593Smuzhiyuninspection tool can inspect all programs and maps. 591*4882a593Smuzhiyun 592*4882a593Smuzhiyun3.5 BPF_{PROG,MAP}_GET_FD_BY_ID 593*4882a593Smuzhiyun=============================== 594*4882a593Smuzhiyun 595*4882a593SmuzhiyunAn introspection tool cannot use id to get details about program or maps. 596*4882a593SmuzhiyunA file descriptor needs to be obtained first for reference-counting purpose. 597*4882a593Smuzhiyun 598*4882a593Smuzhiyun3.6 BPF_OBJ_GET_INFO_BY_FD 599*4882a593Smuzhiyun========================== 600*4882a593Smuzhiyun 601*4882a593SmuzhiyunOnce a program/map fd is acquired, an introspection tool can get the detailed 602*4882a593Smuzhiyuninformation from kernel about this fd, some of which are BTF-related. For 603*4882a593Smuzhiyunexample, ``bpf_map_info`` returns ``btf_id`` and key/value type ids. 604*4882a593Smuzhiyun``bpf_prog_info`` returns ``btf_id``, func_info, and line info for translated 605*4882a593Smuzhiyunbpf byte codes, and jited_line_info. 606*4882a593Smuzhiyun 607*4882a593Smuzhiyun3.7 BPF_BTF_GET_FD_BY_ID 608*4882a593Smuzhiyun======================== 609*4882a593Smuzhiyun 610*4882a593SmuzhiyunWith ``btf_id`` obtained in ``bpf_map_info`` and ``bpf_prog_info``, bpf 611*4882a593Smuzhiyunsyscall command BPF_BTF_GET_FD_BY_ID can retrieve a btf fd. Then, with 612*4882a593Smuzhiyuncommand BPF_OBJ_GET_INFO_BY_FD, the btf blob, originally loaded into the 613*4882a593Smuzhiyunkernel with BPF_BTF_LOAD, can be retrieved. 614*4882a593Smuzhiyun 615*4882a593SmuzhiyunWith the btf blob, ``bpf_map_info``, and ``bpf_prog_info``, an introspection 616*4882a593Smuzhiyuntool has full btf knowledge and is able to pretty print map key/values, dump 617*4882a593Smuzhiyunfunc signatures and line info, along with byte/jit codes. 618*4882a593Smuzhiyun 619*4882a593Smuzhiyun4. ELF File Format Interface 620*4882a593Smuzhiyun**************************** 621*4882a593Smuzhiyun 622*4882a593Smuzhiyun4.1 .BTF section 623*4882a593Smuzhiyun================ 624*4882a593Smuzhiyun 625*4882a593SmuzhiyunThe .BTF section contains type and string data. The format of this section is 626*4882a593Smuzhiyunsame as the one describe in :ref:`BTF_Type_String`. 627*4882a593Smuzhiyun 628*4882a593Smuzhiyun.. _BTF_Ext_Section: 629*4882a593Smuzhiyun 630*4882a593Smuzhiyun4.2 .BTF.ext section 631*4882a593Smuzhiyun==================== 632*4882a593Smuzhiyun 633*4882a593SmuzhiyunThe .BTF.ext section encodes func_info and line_info which needs loader 634*4882a593Smuzhiyunmanipulation before loading into the kernel. 635*4882a593Smuzhiyun 636*4882a593SmuzhiyunThe specification for .BTF.ext section is defined at ``tools/lib/bpf/btf.h`` 637*4882a593Smuzhiyunand ``tools/lib/bpf/btf.c``. 638*4882a593Smuzhiyun 639*4882a593SmuzhiyunThe current header of .BTF.ext section:: 640*4882a593Smuzhiyun 641*4882a593Smuzhiyun struct btf_ext_header { 642*4882a593Smuzhiyun __u16 magic; 643*4882a593Smuzhiyun __u8 version; 644*4882a593Smuzhiyun __u8 flags; 645*4882a593Smuzhiyun __u32 hdr_len; 646*4882a593Smuzhiyun 647*4882a593Smuzhiyun /* All offsets are in bytes relative to the end of this header */ 648*4882a593Smuzhiyun __u32 func_info_off; 649*4882a593Smuzhiyun __u32 func_info_len; 650*4882a593Smuzhiyun __u32 line_info_off; 651*4882a593Smuzhiyun __u32 line_info_len; 652*4882a593Smuzhiyun }; 653*4882a593Smuzhiyun 654*4882a593SmuzhiyunIt is very similar to .BTF section. Instead of type/string section, it 655*4882a593Smuzhiyuncontains func_info and line_info section. See :ref:`BPF_Prog_Load` for details 656*4882a593Smuzhiyunabout func_info and line_info record format. 657*4882a593Smuzhiyun 658*4882a593SmuzhiyunThe func_info is organized as below.:: 659*4882a593Smuzhiyun 660*4882a593Smuzhiyun func_info_rec_size 661*4882a593Smuzhiyun btf_ext_info_sec for section #1 /* func_info for section #1 */ 662*4882a593Smuzhiyun btf_ext_info_sec for section #2 /* func_info for section #2 */ 663*4882a593Smuzhiyun ... 664*4882a593Smuzhiyun 665*4882a593Smuzhiyun``func_info_rec_size`` specifies the size of ``bpf_func_info`` structure when 666*4882a593Smuzhiyun.BTF.ext is generated. ``btf_ext_info_sec``, defined below, is a collection of 667*4882a593Smuzhiyunfunc_info for each specific ELF section.:: 668*4882a593Smuzhiyun 669*4882a593Smuzhiyun struct btf_ext_info_sec { 670*4882a593Smuzhiyun __u32 sec_name_off; /* offset to section name */ 671*4882a593Smuzhiyun __u32 num_info; 672*4882a593Smuzhiyun /* Followed by num_info * record_size number of bytes */ 673*4882a593Smuzhiyun __u8 data[0]; 674*4882a593Smuzhiyun }; 675*4882a593Smuzhiyun 676*4882a593SmuzhiyunHere, num_info must be greater than 0. 677*4882a593Smuzhiyun 678*4882a593SmuzhiyunThe line_info is organized as below.:: 679*4882a593Smuzhiyun 680*4882a593Smuzhiyun line_info_rec_size 681*4882a593Smuzhiyun btf_ext_info_sec for section #1 /* line_info for section #1 */ 682*4882a593Smuzhiyun btf_ext_info_sec for section #2 /* line_info for section #2 */ 683*4882a593Smuzhiyun ... 684*4882a593Smuzhiyun 685*4882a593Smuzhiyun``line_info_rec_size`` specifies the size of ``bpf_line_info`` structure when 686*4882a593Smuzhiyun.BTF.ext is generated. 687*4882a593Smuzhiyun 688*4882a593SmuzhiyunThe interpretation of ``bpf_func_info->insn_off`` and 689*4882a593Smuzhiyun``bpf_line_info->insn_off`` is different between kernel API and ELF API. For 690*4882a593Smuzhiyunkernel API, the ``insn_off`` is the instruction offset in the unit of ``struct 691*4882a593Smuzhiyunbpf_insn``. For ELF API, the ``insn_off`` is the byte offset from the 692*4882a593Smuzhiyunbeginning of section (``btf_ext_info_sec->sec_name_off``). 693*4882a593Smuzhiyun 694*4882a593Smuzhiyun4.2 .BTF_ids section 695*4882a593Smuzhiyun==================== 696*4882a593Smuzhiyun 697*4882a593SmuzhiyunThe .BTF_ids section encodes BTF ID values that are used within the kernel. 698*4882a593Smuzhiyun 699*4882a593SmuzhiyunThis section is created during the kernel compilation with the help of 700*4882a593Smuzhiyunmacros defined in ``include/linux/btf_ids.h`` header file. Kernel code can 701*4882a593Smuzhiyunuse them to create lists and sets (sorted lists) of BTF ID values. 702*4882a593Smuzhiyun 703*4882a593SmuzhiyunThe ``BTF_ID_LIST`` and ``BTF_ID`` macros define unsorted list of BTF ID values, 704*4882a593Smuzhiyunwith following syntax:: 705*4882a593Smuzhiyun 706*4882a593Smuzhiyun BTF_ID_LIST(list) 707*4882a593Smuzhiyun BTF_ID(type1, name1) 708*4882a593Smuzhiyun BTF_ID(type2, name2) 709*4882a593Smuzhiyun 710*4882a593Smuzhiyunresulting in following layout in .BTF_ids section:: 711*4882a593Smuzhiyun 712*4882a593Smuzhiyun __BTF_ID__type1__name1__1: 713*4882a593Smuzhiyun .zero 4 714*4882a593Smuzhiyun __BTF_ID__type2__name2__2: 715*4882a593Smuzhiyun .zero 4 716*4882a593Smuzhiyun 717*4882a593SmuzhiyunThe ``u32 list[];`` variable is defined to access the list. 718*4882a593Smuzhiyun 719*4882a593SmuzhiyunThe ``BTF_ID_UNUSED`` macro defines 4 zero bytes. It's used when we 720*4882a593Smuzhiyunwant to define unused entry in BTF_ID_LIST, like:: 721*4882a593Smuzhiyun 722*4882a593Smuzhiyun BTF_ID_LIST(bpf_skb_output_btf_ids) 723*4882a593Smuzhiyun BTF_ID(struct, sk_buff) 724*4882a593Smuzhiyun BTF_ID_UNUSED 725*4882a593Smuzhiyun BTF_ID(struct, task_struct) 726*4882a593Smuzhiyun 727*4882a593SmuzhiyunThe ``BTF_SET_START/END`` macros pair defines sorted list of BTF ID values 728*4882a593Smuzhiyunand their count, with following syntax:: 729*4882a593Smuzhiyun 730*4882a593Smuzhiyun BTF_SET_START(set) 731*4882a593Smuzhiyun BTF_ID(type1, name1) 732*4882a593Smuzhiyun BTF_ID(type2, name2) 733*4882a593Smuzhiyun BTF_SET_END(set) 734*4882a593Smuzhiyun 735*4882a593Smuzhiyunresulting in following layout in .BTF_ids section:: 736*4882a593Smuzhiyun 737*4882a593Smuzhiyun __BTF_ID__set__set: 738*4882a593Smuzhiyun .zero 4 739*4882a593Smuzhiyun __BTF_ID__type1__name1__3: 740*4882a593Smuzhiyun .zero 4 741*4882a593Smuzhiyun __BTF_ID__type2__name2__4: 742*4882a593Smuzhiyun .zero 4 743*4882a593Smuzhiyun 744*4882a593SmuzhiyunThe ``struct btf_id_set set;`` variable is defined to access the list. 745*4882a593Smuzhiyun 746*4882a593SmuzhiyunThe ``typeX`` name can be one of following:: 747*4882a593Smuzhiyun 748*4882a593Smuzhiyun struct, union, typedef, func 749*4882a593Smuzhiyun 750*4882a593Smuzhiyunand is used as a filter when resolving the BTF ID value. 751*4882a593Smuzhiyun 752*4882a593SmuzhiyunAll the BTF ID lists and sets are compiled in the .BTF_ids section and 753*4882a593Smuzhiyunresolved during the linking phase of kernel build by ``resolve_btfids`` tool. 754*4882a593Smuzhiyun 755*4882a593Smuzhiyun5. Using BTF 756*4882a593Smuzhiyun************ 757*4882a593Smuzhiyun 758*4882a593Smuzhiyun5.1 bpftool map pretty print 759*4882a593Smuzhiyun============================ 760*4882a593Smuzhiyun 761*4882a593SmuzhiyunWith BTF, the map key/value can be printed based on fields rather than simply 762*4882a593Smuzhiyunraw bytes. This is especially valuable for large structure or if your data 763*4882a593Smuzhiyunstructure has bitfields. For example, for the following map,:: 764*4882a593Smuzhiyun 765*4882a593Smuzhiyun enum A { A1, A2, A3, A4, A5 }; 766*4882a593Smuzhiyun typedef enum A ___A; 767*4882a593Smuzhiyun struct tmp_t { 768*4882a593Smuzhiyun char a1:4; 769*4882a593Smuzhiyun int a2:4; 770*4882a593Smuzhiyun int :4; 771*4882a593Smuzhiyun __u32 a3:4; 772*4882a593Smuzhiyun int b; 773*4882a593Smuzhiyun ___A b1:4; 774*4882a593Smuzhiyun enum A b2:4; 775*4882a593Smuzhiyun }; 776*4882a593Smuzhiyun struct bpf_map_def SEC("maps") tmpmap = { 777*4882a593Smuzhiyun .type = BPF_MAP_TYPE_ARRAY, 778*4882a593Smuzhiyun .key_size = sizeof(__u32), 779*4882a593Smuzhiyun .value_size = sizeof(struct tmp_t), 780*4882a593Smuzhiyun .max_entries = 1, 781*4882a593Smuzhiyun }; 782*4882a593Smuzhiyun BPF_ANNOTATE_KV_PAIR(tmpmap, int, struct tmp_t); 783*4882a593Smuzhiyun 784*4882a593Smuzhiyunbpftool is able to pretty print like below: 785*4882a593Smuzhiyun:: 786*4882a593Smuzhiyun 787*4882a593Smuzhiyun [{ 788*4882a593Smuzhiyun "key": 0, 789*4882a593Smuzhiyun "value": { 790*4882a593Smuzhiyun "a1": 0x2, 791*4882a593Smuzhiyun "a2": 0x4, 792*4882a593Smuzhiyun "a3": 0x6, 793*4882a593Smuzhiyun "b": 7, 794*4882a593Smuzhiyun "b1": 0x8, 795*4882a593Smuzhiyun "b2": 0xa 796*4882a593Smuzhiyun } 797*4882a593Smuzhiyun } 798*4882a593Smuzhiyun ] 799*4882a593Smuzhiyun 800*4882a593Smuzhiyun5.2 bpftool prog dump 801*4882a593Smuzhiyun===================== 802*4882a593Smuzhiyun 803*4882a593SmuzhiyunThe following is an example showing how func_info and line_info can help prog 804*4882a593Smuzhiyundump with better kernel symbol names, function prototypes and line 805*4882a593Smuzhiyuninformation.:: 806*4882a593Smuzhiyun 807*4882a593Smuzhiyun $ bpftool prog dump jited pinned /sys/fs/bpf/test_btf_haskv 808*4882a593Smuzhiyun [...] 809*4882a593Smuzhiyun int test_long_fname_2(struct dummy_tracepoint_args * arg): 810*4882a593Smuzhiyun bpf_prog_44a040bf25481309_test_long_fname_2: 811*4882a593Smuzhiyun ; static int test_long_fname_2(struct dummy_tracepoint_args *arg) 812*4882a593Smuzhiyun 0: push %rbp 813*4882a593Smuzhiyun 1: mov %rsp,%rbp 814*4882a593Smuzhiyun 4: sub $0x30,%rsp 815*4882a593Smuzhiyun b: sub $0x28,%rbp 816*4882a593Smuzhiyun f: mov %rbx,0x0(%rbp) 817*4882a593Smuzhiyun 13: mov %r13,0x8(%rbp) 818*4882a593Smuzhiyun 17: mov %r14,0x10(%rbp) 819*4882a593Smuzhiyun 1b: mov %r15,0x18(%rbp) 820*4882a593Smuzhiyun 1f: xor %eax,%eax 821*4882a593Smuzhiyun 21: mov %rax,0x20(%rbp) 822*4882a593Smuzhiyun 25: xor %esi,%esi 823*4882a593Smuzhiyun ; int key = 0; 824*4882a593Smuzhiyun 27: mov %esi,-0x4(%rbp) 825*4882a593Smuzhiyun ; if (!arg->sock) 826*4882a593Smuzhiyun 2a: mov 0x8(%rdi),%rdi 827*4882a593Smuzhiyun ; if (!arg->sock) 828*4882a593Smuzhiyun 2e: cmp $0x0,%rdi 829*4882a593Smuzhiyun 32: je 0x0000000000000070 830*4882a593Smuzhiyun 34: mov %rbp,%rsi 831*4882a593Smuzhiyun ; counts = bpf_map_lookup_elem(&btf_map, &key); 832*4882a593Smuzhiyun [...] 833*4882a593Smuzhiyun 834*4882a593Smuzhiyun5.3 Verifier Log 835*4882a593Smuzhiyun================ 836*4882a593Smuzhiyun 837*4882a593SmuzhiyunThe following is an example of how line_info can help debugging verification 838*4882a593Smuzhiyunfailure.:: 839*4882a593Smuzhiyun 840*4882a593Smuzhiyun /* The code at tools/testing/selftests/bpf/test_xdp_noinline.c 841*4882a593Smuzhiyun * is modified as below. 842*4882a593Smuzhiyun */ 843*4882a593Smuzhiyun data = (void *)(long)xdp->data; 844*4882a593Smuzhiyun data_end = (void *)(long)xdp->data_end; 845*4882a593Smuzhiyun /* 846*4882a593Smuzhiyun if (data + 4 > data_end) 847*4882a593Smuzhiyun return XDP_DROP; 848*4882a593Smuzhiyun */ 849*4882a593Smuzhiyun *(u32 *)data = dst->dst; 850*4882a593Smuzhiyun 851*4882a593Smuzhiyun $ bpftool prog load ./test_xdp_noinline.o /sys/fs/bpf/test_xdp_noinline type xdp 852*4882a593Smuzhiyun ; data = (void *)(long)xdp->data; 853*4882a593Smuzhiyun 224: (79) r2 = *(u64 *)(r10 -112) 854*4882a593Smuzhiyun 225: (61) r2 = *(u32 *)(r2 +0) 855*4882a593Smuzhiyun ; *(u32 *)data = dst->dst; 856*4882a593Smuzhiyun 226: (63) *(u32 *)(r2 +0) = r1 857*4882a593Smuzhiyun invalid access to packet, off=0 size=4, R2(id=0,off=0,r=0) 858*4882a593Smuzhiyun R2 offset is outside of the packet 859*4882a593Smuzhiyun 860*4882a593Smuzhiyun6. BTF Generation 861*4882a593Smuzhiyun***************** 862*4882a593Smuzhiyun 863*4882a593SmuzhiyunYou need latest pahole 864*4882a593Smuzhiyun 865*4882a593Smuzhiyun https://git.kernel.org/pub/scm/devel/pahole/pahole.git/ 866*4882a593Smuzhiyun 867*4882a593Smuzhiyunor llvm (8.0 or later). The pahole acts as a dwarf2btf converter. It doesn't 868*4882a593Smuzhiyunsupport .BTF.ext and btf BTF_KIND_FUNC type yet. For example,:: 869*4882a593Smuzhiyun 870*4882a593Smuzhiyun -bash-4.4$ cat t.c 871*4882a593Smuzhiyun struct t { 872*4882a593Smuzhiyun int a:2; 873*4882a593Smuzhiyun int b:3; 874*4882a593Smuzhiyun int c:2; 875*4882a593Smuzhiyun } g; 876*4882a593Smuzhiyun -bash-4.4$ gcc -c -O2 -g t.c 877*4882a593Smuzhiyun -bash-4.4$ pahole -JV t.o 878*4882a593Smuzhiyun File t.o: 879*4882a593Smuzhiyun [1] STRUCT t kind_flag=1 size=4 vlen=3 880*4882a593Smuzhiyun a type_id=2 bitfield_size=2 bits_offset=0 881*4882a593Smuzhiyun b type_id=2 bitfield_size=3 bits_offset=2 882*4882a593Smuzhiyun c type_id=2 bitfield_size=2 bits_offset=5 883*4882a593Smuzhiyun [2] INT int size=4 bit_offset=0 nr_bits=32 encoding=SIGNED 884*4882a593Smuzhiyun 885*4882a593SmuzhiyunThe llvm is able to generate .BTF and .BTF.ext directly with -g for bpf target 886*4882a593Smuzhiyunonly. The assembly code (-S) is able to show the BTF encoding in assembly 887*4882a593Smuzhiyunformat.:: 888*4882a593Smuzhiyun 889*4882a593Smuzhiyun -bash-4.4$ cat t2.c 890*4882a593Smuzhiyun typedef int __int32; 891*4882a593Smuzhiyun struct t2 { 892*4882a593Smuzhiyun int a2; 893*4882a593Smuzhiyun int (*f2)(char q1, __int32 q2, ...); 894*4882a593Smuzhiyun int (*f3)(); 895*4882a593Smuzhiyun } g2; 896*4882a593Smuzhiyun int main() { return 0; } 897*4882a593Smuzhiyun int test() { return 0; } 898*4882a593Smuzhiyun -bash-4.4$ clang -c -g -O2 -target bpf t2.c 899*4882a593Smuzhiyun -bash-4.4$ readelf -S t2.o 900*4882a593Smuzhiyun ...... 901*4882a593Smuzhiyun [ 8] .BTF PROGBITS 0000000000000000 00000247 902*4882a593Smuzhiyun 000000000000016e 0000000000000000 0 0 1 903*4882a593Smuzhiyun [ 9] .BTF.ext PROGBITS 0000000000000000 000003b5 904*4882a593Smuzhiyun 0000000000000060 0000000000000000 0 0 1 905*4882a593Smuzhiyun [10] .rel.BTF.ext REL 0000000000000000 000007e0 906*4882a593Smuzhiyun 0000000000000040 0000000000000010 16 9 8 907*4882a593Smuzhiyun ...... 908*4882a593Smuzhiyun -bash-4.4$ clang -S -g -O2 -target bpf t2.c 909*4882a593Smuzhiyun -bash-4.4$ cat t2.s 910*4882a593Smuzhiyun ...... 911*4882a593Smuzhiyun .section .BTF,"",@progbits 912*4882a593Smuzhiyun .short 60319 # 0xeb9f 913*4882a593Smuzhiyun .byte 1 914*4882a593Smuzhiyun .byte 0 915*4882a593Smuzhiyun .long 24 916*4882a593Smuzhiyun .long 0 917*4882a593Smuzhiyun .long 220 918*4882a593Smuzhiyun .long 220 919*4882a593Smuzhiyun .long 122 920*4882a593Smuzhiyun .long 0 # BTF_KIND_FUNC_PROTO(id = 1) 921*4882a593Smuzhiyun .long 218103808 # 0xd000000 922*4882a593Smuzhiyun .long 2 923*4882a593Smuzhiyun .long 83 # BTF_KIND_INT(id = 2) 924*4882a593Smuzhiyun .long 16777216 # 0x1000000 925*4882a593Smuzhiyun .long 4 926*4882a593Smuzhiyun .long 16777248 # 0x1000020 927*4882a593Smuzhiyun ...... 928*4882a593Smuzhiyun .byte 0 # string offset=0 929*4882a593Smuzhiyun .ascii ".text" # string offset=1 930*4882a593Smuzhiyun .byte 0 931*4882a593Smuzhiyun .ascii "/home/yhs/tmp-pahole/t2.c" # string offset=7 932*4882a593Smuzhiyun .byte 0 933*4882a593Smuzhiyun .ascii "int main() { return 0; }" # string offset=33 934*4882a593Smuzhiyun .byte 0 935*4882a593Smuzhiyun .ascii "int test() { return 0; }" # string offset=58 936*4882a593Smuzhiyun .byte 0 937*4882a593Smuzhiyun .ascii "int" # string offset=83 938*4882a593Smuzhiyun ...... 939*4882a593Smuzhiyun .section .BTF.ext,"",@progbits 940*4882a593Smuzhiyun .short 60319 # 0xeb9f 941*4882a593Smuzhiyun .byte 1 942*4882a593Smuzhiyun .byte 0 943*4882a593Smuzhiyun .long 24 944*4882a593Smuzhiyun .long 0 945*4882a593Smuzhiyun .long 28 946*4882a593Smuzhiyun .long 28 947*4882a593Smuzhiyun .long 44 948*4882a593Smuzhiyun .long 8 # FuncInfo 949*4882a593Smuzhiyun .long 1 # FuncInfo section string offset=1 950*4882a593Smuzhiyun .long 2 951*4882a593Smuzhiyun .long .Lfunc_begin0 952*4882a593Smuzhiyun .long 3 953*4882a593Smuzhiyun .long .Lfunc_begin1 954*4882a593Smuzhiyun .long 5 955*4882a593Smuzhiyun .long 16 # LineInfo 956*4882a593Smuzhiyun .long 1 # LineInfo section string offset=1 957*4882a593Smuzhiyun .long 2 958*4882a593Smuzhiyun .long .Ltmp0 959*4882a593Smuzhiyun .long 7 960*4882a593Smuzhiyun .long 33 961*4882a593Smuzhiyun .long 7182 # Line 7 Col 14 962*4882a593Smuzhiyun .long .Ltmp3 963*4882a593Smuzhiyun .long 7 964*4882a593Smuzhiyun .long 58 965*4882a593Smuzhiyun .long 8206 # Line 8 Col 14 966*4882a593Smuzhiyun 967*4882a593Smuzhiyun7. Testing 968*4882a593Smuzhiyun********** 969*4882a593Smuzhiyun 970*4882a593SmuzhiyunKernel bpf selftest `test_btf.c` provides extensive set of BTF-related tests. 971