bpftool-btf.rst 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. .. SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
  2. ================
  3. bpftool-btf
  4. ================
  5. -------------------------------------------------------------------------------
  6. tool for inspection of BTF data
  7. -------------------------------------------------------------------------------
  8. :Manual section: 8
  9. .. include:: substitutions.rst
  10. SYNOPSIS
  11. ========
  12. **bpftool** [*OPTIONS*] **btf** *COMMAND*
  13. *OPTIONS* := { |COMMON_OPTIONS| | { **-B** | **--base-btf** } }
  14. *COMMANDS* := { **dump** | **help** }
  15. BTF COMMANDS
  16. =============
  17. | **bpftool** **btf** { **show** | **list** } [**id** *BTF_ID*]
  18. | **bpftool** **btf dump** *BTF_SRC* [**format** *FORMAT*]
  19. | **bpftool** **btf help**
  20. |
  21. | *BTF_SRC* := { **id** *BTF_ID* | **prog** *PROG* | **map** *MAP* [{**key** | **value** | **kv** | **all**}] | **file** *FILE* }
  22. | *FORMAT* := { **raw** | **c** }
  23. | *MAP* := { **id** *MAP_ID* | **pinned** *FILE* }
  24. | *PROG* := { **id** *PROG_ID* | **pinned** *FILE* | **tag** *PROG_TAG* }
  25. DESCRIPTION
  26. ===========
  27. **bpftool btf { show | list }** [**id** *BTF_ID*]
  28. Show information about loaded BTF objects. If a BTF ID is
  29. specified, show information only about given BTF object,
  30. otherwise list all BTF objects currently loaded on the
  31. system.
  32. Since Linux 5.8 bpftool is able to discover information about
  33. processes that hold open file descriptors (FDs) against BTF
  34. objects. On such kernels bpftool will automatically emit this
  35. information as well.
  36. **bpftool btf dump** *BTF_SRC*
  37. Dump BTF entries from a given *BTF_SRC*.
  38. When **id** is specified, BTF object with that ID will be
  39. loaded and all its BTF types emitted.
  40. When **map** is provided, it's expected that map has
  41. associated BTF object with BTF types describing key and
  42. value. It's possible to select whether to dump only BTF
  43. type(s) associated with key (**key**), value (**value**),
  44. both key and value (**kv**), or all BTF types present in
  45. associated BTF object (**all**). If not specified, **kv**
  46. is assumed.
  47. When **prog** is provided, it's expected that program has
  48. associated BTF object with BTF types.
  49. When specifying *FILE*, an ELF file is expected, containing
  50. .BTF section with well-defined BTF binary format data,
  51. typically produced by clang or pahole.
  52. **format** option can be used to override default (raw)
  53. output format. Raw (**raw**) or C-syntax (**c**) output
  54. formats are supported.
  55. **bpftool btf help**
  56. Print short help message.
  57. OPTIONS
  58. =======
  59. .. include:: common_options.rst
  60. -B, --base-btf *FILE*
  61. Pass a base BTF object. Base BTF objects are typically used
  62. with BTF objects for kernel modules. To avoid duplicating
  63. all kernel symbols required by modules, BTF objects for
  64. modules are "split", they are built incrementally on top of
  65. the kernel (vmlinux) BTF object. So the base BTF reference
  66. should usually point to the kernel BTF.
  67. When the main BTF object to process (for example, the
  68. module BTF to dump) is passed as a *FILE*, bpftool attempts
  69. to autodetect the path for the base object, and passing
  70. this option is optional. When the main BTF object is passed
  71. through other handles, this option becomes necessary.
  72. EXAMPLES
  73. ========
  74. **# bpftool btf dump id 1226**
  75. ::
  76. [1] PTR '(anon)' type_id=2
  77. [2] STRUCT 'dummy_tracepoint_args' size=16 vlen=2
  78. 'pad' type_id=3 bits_offset=0
  79. 'sock' type_id=4 bits_offset=64
  80. [3] INT 'long long unsigned int' size=8 bits_offset=0 nr_bits=64 encoding=(none)
  81. [4] PTR '(anon)' type_id=5
  82. [5] FWD 'sock' fwd_kind=union
  83. This gives an example of default output for all supported BTF kinds.
  84. **$ cat prog.c**
  85. ::
  86. struct fwd_struct;
  87. enum my_enum {
  88. VAL1 = 3,
  89. VAL2 = 7,
  90. };
  91. typedef struct my_struct my_struct_t;
  92. struct my_struct {
  93. const unsigned int const_int_field;
  94. int bitfield_field: 4;
  95. char arr_field[16];
  96. const struct fwd_struct *restrict fwd_field;
  97. enum my_enum enum_field;
  98. volatile my_struct_t *typedef_ptr_field;
  99. };
  100. union my_union {
  101. int a;
  102. struct my_struct b;
  103. };
  104. struct my_struct struct_global_var __attribute__((section("data_sec"))) = {
  105. .bitfield_field = 3,
  106. .enum_field = VAL1,
  107. };
  108. int global_var __attribute__((section("data_sec"))) = 7;
  109. __attribute__((noinline))
  110. int my_func(union my_union *arg1, int arg2)
  111. {
  112. static int static_var __attribute__((section("data_sec"))) = 123;
  113. static_var++;
  114. return static_var;
  115. }
  116. **$ bpftool btf dump file prog.o**
  117. ::
  118. [1] PTR '(anon)' type_id=2
  119. [2] UNION 'my_union' size=48 vlen=2
  120. 'a' type_id=3 bits_offset=0
  121. 'b' type_id=4 bits_offset=0
  122. [3] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED
  123. [4] STRUCT 'my_struct' size=48 vlen=6
  124. 'const_int_field' type_id=5 bits_offset=0
  125. 'bitfield_field' type_id=3 bits_offset=32 bitfield_size=4
  126. 'arr_field' type_id=8 bits_offset=40
  127. 'fwd_field' type_id=10 bits_offset=192
  128. 'enum_field' type_id=14 bits_offset=256
  129. 'typedef_ptr_field' type_id=15 bits_offset=320
  130. [5] CONST '(anon)' type_id=6
  131. [6] INT 'unsigned int' size=4 bits_offset=0 nr_bits=32 encoding=(none)
  132. [7] INT 'char' size=1 bits_offset=0 nr_bits=8 encoding=SIGNED
  133. [8] ARRAY '(anon)' type_id=7 index_type_id=9 nr_elems=16
  134. [9] INT '__ARRAY_SIZE_TYPE__' size=4 bits_offset=0 nr_bits=32 encoding=(none)
  135. [10] RESTRICT '(anon)' type_id=11
  136. [11] PTR '(anon)' type_id=12
  137. [12] CONST '(anon)' type_id=13
  138. [13] FWD 'fwd_struct' fwd_kind=union
  139. [14] ENUM 'my_enum' size=4 vlen=2
  140. 'VAL1' val=3
  141. 'VAL2' val=7
  142. [15] PTR '(anon)' type_id=16
  143. [16] VOLATILE '(anon)' type_id=17
  144. [17] TYPEDEF 'my_struct_t' type_id=4
  145. [18] FUNC_PROTO '(anon)' ret_type_id=3 vlen=2
  146. 'arg1' type_id=1
  147. 'arg2' type_id=3
  148. [19] FUNC 'my_func' type_id=18
  149. [20] VAR 'struct_global_var' type_id=4, linkage=global-alloc
  150. [21] VAR 'global_var' type_id=3, linkage=global-alloc
  151. [22] VAR 'my_func.static_var' type_id=3, linkage=static
  152. [23] DATASEC 'data_sec' size=0 vlen=3
  153. type_id=20 offset=0 size=48
  154. type_id=21 offset=0 size=4
  155. type_id=22 offset=52 size=4
  156. The following commands print BTF types associated with specified map's key,
  157. value, both key and value, and all BTF types, respectively. By default, both
  158. key and value types will be printed.
  159. **# bpftool btf dump map id 123 key**
  160. ::
  161. [39] TYPEDEF 'u32' type_id=37
  162. **# bpftool btf dump map id 123 value**
  163. ::
  164. [86] PTR '(anon)' type_id=87
  165. **# bpftool btf dump map id 123 kv**
  166. ::
  167. [39] TYPEDEF 'u32' type_id=37
  168. [86] PTR '(anon)' type_id=87
  169. **# bpftool btf dump map id 123 all**
  170. ::
  171. [1] PTR '(anon)' type_id=0
  172. .
  173. .
  174. .
  175. [2866] ARRAY '(anon)' type_id=52 index_type_id=51 nr_elems=4
  176. All the standard ways to specify map or program are supported:
  177. **# bpftool btf dump map id 123**
  178. **# bpftool btf dump map pinned /sys/fs/bpf/map_name**
  179. **# bpftool btf dump prog id 456**
  180. **# bpftool btf dump prog tag b88e0a09b1d9759d**
  181. **# bpftool btf dump prog pinned /sys/fs/bpf/prog_name**
  182. |
  183. | **# bpftool btf dump file /sys/kernel/btf/i2c_smbus**
  184. | (or)
  185. | **# I2C_SMBUS_ID=$(bpftool btf show -p | jq '.[] | select(.name=="i2c_smbus").id')**
  186. | **# bpftool btf dump id ${I2C_SMBUS_ID} -B /sys/kernel/btf/vmlinux**
  187. ::
  188. [104848] STRUCT 'i2c_smbus_alert' size=40 vlen=2
  189. 'alert' type_id=393 bits_offset=0
  190. 'ara' type_id=56050 bits_offset=256
  191. [104849] STRUCT 'alert_data' size=12 vlen=3
  192. 'addr' type_id=16 bits_offset=0
  193. 'type' type_id=56053 bits_offset=32
  194. 'data' type_id=7 bits_offset=64
  195. [104850] PTR '(anon)' type_id=104848
  196. [104851] PTR '(anon)' type_id=104849
  197. [104852] FUNC 'i2c_register_spd' type_id=84745 linkage=static
  198. [104853] FUNC 'smbalert_driver_init' type_id=1213 linkage=static
  199. [104854] FUNC_PROTO '(anon)' ret_type_id=18 vlen=1
  200. 'ara' type_id=56050
  201. [104855] FUNC 'i2c_handle_smbus_alert' type_id=104854 linkage=static
  202. [104856] FUNC 'smbalert_remove' type_id=104854 linkage=static
  203. [104857] FUNC_PROTO '(anon)' ret_type_id=18 vlen=2
  204. 'ara' type_id=56050
  205. 'id' type_id=56056
  206. [104858] FUNC 'smbalert_probe' type_id=104857 linkage=static
  207. [104859] FUNC 'smbalert_work' type_id=9695 linkage=static
  208. [104860] FUNC 'smbus_alert' type_id=71367 linkage=static
  209. [104861] FUNC 'smbus_do_alert' type_id=84827 linkage=static