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