printk-formats.rst 13 KB

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