printk-formats.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. =========================================
  2. How to get printk format specifiers right
  3. =========================================
  4. .. _printk-specifiers:
  5. :Author: Randy Dunlap <rdunlap@infradead.org>
  6. :Author: Andrew Murray <amurray@mpc-data.co.uk>
  7. Integer types
  8. =============
  9. ::
  10. If variable is of Type, use printk format specifier:
  11. ------------------------------------------------------------
  12. char %d or %x
  13. unsigned char %u or %x
  14. short int %d or %x
  15. unsigned short int %u or %x
  16. int %d or %x
  17. unsigned int %u or %x
  18. long %ld or %lx
  19. unsigned long %lu or %lx
  20. long long %lld or %llx
  21. unsigned long long %llu or %llx
  22. size_t %zu or %zx
  23. ssize_t %zd or %zx
  24. s8 %d or %x
  25. u8 %u or %x
  26. s16 %d or %x
  27. u16 %u or %x
  28. s32 %d or %x
  29. u32 %u or %x
  30. s64 %lld or %llx
  31. u64 %llu or %llx
  32. If <type> is dependent on a config option for its size (e.g., sector_t,
  33. blkcnt_t) or is architecture-dependent for its size (e.g., tcflag_t), use a
  34. format specifier of its largest possible type and explicitly cast to it.
  35. Example::
  36. printk("test: sector number/total blocks: %llu/%llu\n",
  37. (unsigned long long)sector, (unsigned long long)blockcount);
  38. Reminder: sizeof() returns type size_t.
  39. The kernel's printf does not support %n. Floating point formats (%e, %f,
  40. %g, %a) are also not recognized, for obvious reasons. Use of any
  41. unsupported specifier or length qualifier results in a WARN and early
  42. return from vsnprintf().
  43. Pointer types
  44. =============
  45. A raw pointer value may be printed with %p which will hash the address
  46. before printing. The kernel also supports extended specifiers for printing
  47. pointers of different types.
  48. Some of the extended specifiers print the data on the given address instead
  49. of printing the address itself. In this case, the following error messages
  50. might be printed instead of the unreachable information::
  51. (null) data on plain NULL address
  52. (efault) data on invalid address
  53. (einval) invalid data on a valid address
  54. Plain Pointers
  55. --------------
  56. ::
  57. %p abcdef12 or 00000000abcdef12
  58. Pointers printed without a specifier extension (i.e unadorned %p) are
  59. hashed to prevent leaking information about the kernel memory layout. This
  60. has the added benefit of providing a unique identifier. On 64-bit machines
  61. the first 32 bits are zeroed. The kernel will print ``(ptrval)`` until it
  62. gathers enough entropy. If you *really* want the address see %px below.
  63. Error Pointers
  64. --------------
  65. ::
  66. %pe -ENOSPC
  67. For printing error pointers (i.e. a pointer for which IS_ERR() is true)
  68. as a symbolic error name. Error values for which no symbolic name is
  69. known are printed in decimal, while a non-ERR_PTR passed as the
  70. argument to %pe gets treated as ordinary %p.
  71. Symbols/Function Pointers
  72. -------------------------
  73. ::
  74. %pS versatile_init+0x0/0x110
  75. %ps versatile_init
  76. %pSR versatile_init+0x9/0x110
  77. (with __builtin_extract_return_addr() translation)
  78. %pB prev_fn_of_versatile_init+0x88/0x88
  79. The ``S`` and ``s`` specifiers are used for printing a pointer in symbolic
  80. format. They result in the symbol name with (S) or without (s)
  81. offsets. If KALLSYMS are disabled then the symbol address is printed instead.
  82. The ``B`` specifier results in the symbol name with offsets and should be
  83. used when printing stack backtraces. The specifier takes into
  84. consideration the effect of compiler optimisations which may occur
  85. when tail-calls are used and marked with the noreturn GCC attribute.
  86. Probed Pointers from BPF / tracing
  87. ----------------------------------
  88. ::
  89. %pks kernel string
  90. %pus user string
  91. The ``k`` and ``u`` specifiers are used for printing prior probed memory from
  92. either kernel memory (k) or user memory (u). The subsequent ``s`` specifier
  93. results in printing a string. For direct use in regular vsnprintf() the (k)
  94. and (u) annotation is ignored, however, when used out of BPF's bpf_trace_printk(),
  95. for example, it reads the memory it is pointing to without faulting.
  96. Kernel Pointers
  97. ---------------
  98. ::
  99. %pK 01234567 or 0123456789abcdef
  100. For printing kernel pointers which should be hidden from unprivileged
  101. users. The behaviour of %pK depends on the kptr_restrict sysctl - see
  102. Documentation/admin-guide/sysctl/kernel.rst for more details.
  103. Unmodified Addresses
  104. --------------------
  105. ::
  106. %px 01234567 or 0123456789abcdef
  107. For printing pointers when you *really* want to print the address. Please
  108. consider whether or not you are leaking sensitive information about the
  109. kernel memory layout before printing pointers with %px. %px is functionally
  110. equivalent to %lx (or %lu). %px is preferred because it is more uniquely
  111. grep'able. If in the future we need to modify the way the kernel handles
  112. printing pointers we will be better equipped to find the call sites.
  113. Pointer Differences
  114. -------------------
  115. ::
  116. %td 2560
  117. %tx a00
  118. For printing the pointer differences, use the %t modifier for ptrdiff_t.
  119. Example::
  120. printk("test: difference between pointers: %td\n", ptr2 - ptr1);
  121. Struct Resources
  122. ----------------
  123. ::
  124. %pr [mem 0x60000000-0x6fffffff flags 0x2200] or
  125. [mem 0x0000000060000000-0x000000006fffffff flags 0x2200]
  126. %pR [mem 0x60000000-0x6fffffff pref] or
  127. [mem 0x0000000060000000-0x000000006fffffff pref]
  128. For printing struct resources. The ``R`` and ``r`` specifiers result in a
  129. printed resource with (R) or without (r) a decoded flags member.
  130. Passed by reference.
  131. Physical address types phys_addr_t
  132. ----------------------------------
  133. ::
  134. %pa[p] 0x01234567 or 0x0123456789abcdef
  135. For printing a phys_addr_t type (and its derivatives, such as
  136. resource_size_t) which can vary based on build options, regardless of the
  137. width of the CPU data path.
  138. Passed by reference.
  139. DMA address types dma_addr_t
  140. ----------------------------
  141. ::
  142. %pad 0x01234567 or 0x0123456789abcdef
  143. For printing a dma_addr_t type which can vary based on build options,
  144. regardless of the width of the CPU data path.
  145. Passed by reference.
  146. Raw buffer as an escaped string
  147. -------------------------------
  148. ::
  149. %*pE[achnops]
  150. For printing raw buffer as an escaped string. For the following buffer::
  151. 1b 62 20 5c 43 07 22 90 0d 5d
  152. A few examples show how the conversion would be done (excluding surrounding
  153. quotes)::
  154. %*pE "\eb \C\a"\220\r]"
  155. %*pEhp "\x1bb \C\x07"\x90\x0d]"
  156. %*pEa "\e\142\040\\\103\a\042\220\r\135"
  157. The conversion rules are applied according to an optional combination
  158. of flags (see :c:func:`string_escape_mem` kernel documentation for the
  159. details):
  160. - a - ESCAPE_ANY
  161. - c - ESCAPE_SPECIAL
  162. - h - ESCAPE_HEX
  163. - n - ESCAPE_NULL
  164. - o - ESCAPE_OCTAL
  165. - p - ESCAPE_NP
  166. - s - ESCAPE_SPACE
  167. By default ESCAPE_ANY_NP is used.
  168. ESCAPE_ANY_NP is the sane choice for many cases, in particularly for
  169. printing SSIDs.
  170. If field width is omitted then 1 byte only will be escaped.
  171. Raw buffer as a hex string
  172. --------------------------
  173. ::
  174. %*ph 00 01 02 ... 3f
  175. %*phC 00:01:02: ... :3f
  176. %*phD 00-01-02- ... -3f
  177. %*phN 000102 ... 3f
  178. For printing small buffers (up to 64 bytes long) as a hex string with a
  179. certain separator. For larger buffers consider using
  180. :c:func:`print_hex_dump`.
  181. MAC/FDDI addresses
  182. ------------------
  183. ::
  184. %pM 00:01:02:03:04:05
  185. %pMR 05:04:03:02:01:00
  186. %pMF 00-01-02-03-04-05
  187. %pm 000102030405
  188. %pmR 050403020100
  189. For printing 6-byte MAC/FDDI addresses in hex notation. The ``M`` and ``m``
  190. specifiers result in a printed address with (M) or without (m) byte
  191. separators. The default byte separator is the colon (:).
  192. Where FDDI addresses are concerned the ``F`` specifier can be used after
  193. the ``M`` specifier to use dash (-) separators instead of the default
  194. separator.
  195. For Bluetooth addresses the ``R`` specifier shall be used after the ``M``
  196. specifier to use reversed byte order suitable for visual interpretation
  197. of Bluetooth addresses which are in the little endian order.
  198. Passed by reference.
  199. IPv4 addresses
  200. --------------
  201. ::
  202. %pI4 1.2.3.4
  203. %pi4 001.002.003.004
  204. %p[Ii]4[hnbl]
  205. For printing IPv4 dot-separated decimal addresses. The ``I4`` and ``i4``
  206. specifiers result in a printed address with (i4) or without (I4) leading
  207. zeros.
  208. The additional ``h``, ``n``, ``b``, and ``l`` specifiers are used to specify
  209. host, network, big or little endian order addresses respectively. Where
  210. no specifier is provided the default network/big endian order is used.
  211. Passed by reference.
  212. IPv6 addresses
  213. --------------
  214. ::
  215. %pI6 0001:0002:0003:0004:0005:0006:0007:0008
  216. %pi6 00010002000300040005000600070008
  217. %pI6c 1:2:3:4:5:6:7:8
  218. For printing IPv6 network-order 16-bit hex addresses. The ``I6`` and ``i6``
  219. specifiers result in a printed address with (I6) or without (i6)
  220. colon-separators. Leading zeros are always used.
  221. The additional ``c`` specifier can be used with the ``I`` specifier to
  222. print a compressed IPv6 address as described by
  223. https://tools.ietf.org/html/rfc5952
  224. Passed by reference.
  225. IPv4/IPv6 addresses (generic, with port, flowinfo, scope)
  226. ---------------------------------------------------------
  227. ::
  228. %pIS 1.2.3.4 or 0001:0002:0003:0004:0005:0006:0007:0008
  229. %piS 001.002.003.004 or 00010002000300040005000600070008
  230. %pISc 1.2.3.4 or 1:2:3:4:5:6:7:8
  231. %pISpc 1.2.3.4:12345 or [1:2:3:4:5:6:7:8]:12345
  232. %p[Ii]S[pfschnbl]
  233. For printing an IP address without the need to distinguish whether it's of
  234. type AF_INET or AF_INET6. A pointer to a valid struct sockaddr,
  235. specified through ``IS`` or ``iS``, can be passed to this format specifier.
  236. The additional ``p``, ``f``, and ``s`` specifiers are used to specify port
  237. (IPv4, IPv6), flowinfo (IPv6) and scope (IPv6). Ports have a ``:`` prefix,
  238. flowinfo a ``/`` and scope a ``%``, each followed by the actual value.
  239. In case of an IPv6 address the compressed IPv6 address as described by
  240. https://tools.ietf.org/html/rfc5952 is being used if the additional
  241. specifier ``c`` is given. The IPv6 address is surrounded by ``[``, ``]`` in
  242. case of additional specifiers ``p``, ``f`` or ``s`` as suggested by
  243. https://tools.ietf.org/html/draft-ietf-6man-text-addr-representation-07
  244. In case of IPv4 addresses, the additional ``h``, ``n``, ``b``, and ``l``
  245. specifiers can be used as well and are ignored in case of an IPv6
  246. address.
  247. Passed by reference.
  248. Further examples::
  249. %pISfc 1.2.3.4 or [1:2:3:4:5:6:7:8]/123456789
  250. %pISsc 1.2.3.4 or [1:2:3:4:5:6:7:8]%1234567890
  251. %pISpfc 1.2.3.4:12345 or [1:2:3:4:5:6:7:8]:12345/123456789
  252. UUID/GUID addresses
  253. -------------------
  254. ::
  255. %pUb 00010203-0405-0607-0809-0a0b0c0d0e0f
  256. %pUB 00010203-0405-0607-0809-0A0B0C0D0E0F
  257. %pUl 03020100-0504-0706-0809-0a0b0c0e0e0f
  258. %pUL 03020100-0504-0706-0809-0A0B0C0E0E0F
  259. For printing 16-byte UUID/GUIDs addresses. The additional ``l``, ``L``,
  260. ``b`` and ``B`` specifiers are used to specify a little endian order in
  261. lower (l) or upper case (L) hex notation - and big endian order in lower (b)
  262. or upper case (B) hex notation.
  263. Where no additional specifiers are used the default big endian
  264. order with lower case hex notation will be printed.
  265. Passed by reference.
  266. dentry names
  267. ------------
  268. ::
  269. %pd{,2,3,4}
  270. %pD{,2,3,4}
  271. For printing dentry name; if we race with :c:func:`d_move`, the name might
  272. be a mix of old and new ones, but it won't oops. %pd dentry is a safer
  273. equivalent of %s dentry->d_name.name we used to use, %pd<n> prints ``n``
  274. last components. %pD does the same thing for struct file.
  275. Passed by reference.
  276. block_device names
  277. ------------------
  278. ::
  279. %pg sda, sda1 or loop0p1
  280. For printing name of block_device pointers.
  281. struct va_format
  282. ----------------
  283. ::
  284. %pV
  285. For printing struct va_format structures. These contain a format string
  286. and va_list as follows::
  287. struct va_format {
  288. const char *fmt;
  289. va_list *va;
  290. };
  291. Implements a "recursive vsnprintf".
  292. Do not use this feature without some mechanism to verify the
  293. correctness of the format string and va_list arguments.
  294. Passed by reference.
  295. Device tree nodes
  296. -----------------
  297. ::
  298. %pOF[fnpPcCF]
  299. For printing device tree node structures. Default behaviour is
  300. equivalent to %pOFf.
  301. - f - device node full_name
  302. - n - device node name
  303. - p - device node phandle
  304. - P - device node path spec (name + @unit)
  305. - F - device node flags
  306. - c - major compatible string
  307. - C - full compatible string
  308. The separator when using multiple arguments is ':'
  309. Examples::
  310. %pOF /foo/bar@0 - Node full name
  311. %pOFf /foo/bar@0 - Same as above
  312. %pOFfp /foo/bar@0:10 - Node full name + phandle
  313. %pOFfcF /foo/bar@0:foo,device:--P- - Node full name +
  314. major compatible string +
  315. node flags
  316. D - dynamic
  317. d - detached
  318. P - Populated
  319. B - Populated bus
  320. Passed by reference.
  321. Fwnode handles
  322. --------------
  323. ::
  324. %pfw[fP]
  325. For printing information on fwnode handles. The default is to print the full
  326. node name, including the path. The modifiers are functionally equivalent to
  327. %pOF above.
  328. - f - full name of the node, including the path
  329. - P - the name of the node including an address (if there is one)
  330. Examples (ACPI)::
  331. %pfwf \_SB.PCI0.CIO2.port@1.endpoint@0 - Full node name
  332. %pfwP endpoint@0 - Node name
  333. Examples (OF)::
  334. %pfwf /ocp@68000000/i2c@48072000/camera@10/port/endpoint - Full name
  335. %pfwP endpoint - Node name
  336. Time and date
  337. -------------
  338. ::
  339. %pt[RT] YYYY-mm-ddTHH:MM:SS
  340. %pt[RT]d YYYY-mm-dd
  341. %pt[RT]t HH:MM:SS
  342. %pt[RT][dt][r]
  343. For printing date and time as represented by::
  344. R struct rtc_time structure
  345. T time64_t type
  346. in human readable format.
  347. By default year will be incremented by 1900 and month by 1.
  348. Use %pt[RT]r (raw) to suppress this behaviour.
  349. Passed by reference.
  350. struct clk
  351. ----------
  352. ::
  353. %pC pll1
  354. %pCn pll1
  355. For printing struct clk structures. %pC and %pCn print the name of the clock
  356. (Common Clock Framework) or a unique 32-bit ID (legacy clock framework).
  357. Passed by reference.
  358. bitmap and its derivatives such as cpumask and nodemask
  359. -------------------------------------------------------
  360. ::
  361. %*pb 0779
  362. %*pbl 0,3-6,8-10
  363. For printing bitmap and its derivatives such as cpumask and nodemask,
  364. %*pb outputs the bitmap with field width as the number of bits and %*pbl
  365. output the bitmap as range list with field width as the number of bits.
  366. Passed by reference.
  367. Flags bitfields such as page flags, gfp_flags
  368. ---------------------------------------------
  369. ::
  370. %pGp referenced|uptodate|lru|active|private
  371. %pGg GFP_USER|GFP_DMA32|GFP_NOWARN
  372. %pGv read|exec|mayread|maywrite|mayexec|denywrite
  373. For printing flags bitfields as a collection of symbolic constants that
  374. would construct the value. The type of flags is given by the third
  375. character. Currently supported are [p]age flags, [v]ma_flags (both
  376. expect ``unsigned long *``) and [g]fp_flags (expects ``gfp_t *``). The flag
  377. names and print order depends on the particular type.
  378. Note that this format should not be used directly in the
  379. :c:func:`TP_printk()` part of a tracepoint. Instead, use the show_*_flags()
  380. functions from <trace/events/mmflags.h>.
  381. Passed by reference.
  382. Network device features
  383. -----------------------
  384. ::
  385. %pNF 0x000000000000c000
  386. For printing netdev_features_t.
  387. Passed by reference.
  388. Thanks
  389. ======
  390. If you add other %p extensions, please extend <lib/test_printf.c> with
  391. one or more test cases, if at all feasible.
  392. Thank you for your cooperation and attention.