Back to home page

OSCL-LXR

 
 

    


0001 =========================================
0002 How to get printk format specifiers right
0003 =========================================
0004 
0005 .. _printk-specifiers:
0006 
0007 :Author: Randy Dunlap <rdunlap@infradead.org>
0008 :Author: Andrew Murray <amurray@mpc-data.co.uk>
0009 
0010 
0011 Integer types
0012 =============
0013 
0014 ::
0015 
0016         If variable is of Type,         use printk format specifier:
0017         ------------------------------------------------------------
0018                 char                    %d or %x
0019                 unsigned char           %u or %x
0020                 short int               %d or %x
0021                 unsigned short int      %u or %x
0022                 int                     %d or %x
0023                 unsigned int            %u or %x
0024                 long                    %ld or %lx
0025                 unsigned long           %lu or %lx
0026                 long long               %lld or %llx
0027                 unsigned long long      %llu or %llx
0028                 size_t                  %zu or %zx
0029                 ssize_t                 %zd or %zx
0030                 s8                      %d or %x
0031                 u8                      %u or %x
0032                 s16                     %d or %x
0033                 u16                     %u or %x
0034                 s32                     %d or %x
0035                 u32                     %u or %x
0036                 s64                     %lld or %llx
0037                 u64                     %llu or %llx
0038 
0039 
0040 If <type> is architecture-dependent for its size (e.g., cycles_t, tcflag_t) or
0041 is dependent on a config option for its size (e.g., blk_status_t), use a format
0042 specifier of its largest possible type and explicitly cast to it.
0043 
0044 Example::
0045 
0046         printk("test: latency: %llu cycles\n", (unsigned long long)time);
0047 
0048 Reminder: sizeof() returns type size_t.
0049 
0050 The kernel's printf does not support %n. Floating point formats (%e, %f,
0051 %g, %a) are also not recognized, for obvious reasons. Use of any
0052 unsupported specifier or length qualifier results in a WARN and early
0053 return from vsnprintf().
0054 
0055 Pointer types
0056 =============
0057 
0058 A raw pointer value may be printed with %p which will hash the address
0059 before printing. The kernel also supports extended specifiers for printing
0060 pointers of different types.
0061 
0062 Some of the extended specifiers print the data on the given address instead
0063 of printing the address itself. In this case, the following error messages
0064 might be printed instead of the unreachable information::
0065 
0066         (null)   data on plain NULL address
0067         (efault) data on invalid address
0068         (einval) invalid data on a valid address
0069 
0070 Plain Pointers
0071 --------------
0072 
0073 ::
0074 
0075         %p      abcdef12 or 00000000abcdef12
0076 
0077 Pointers printed without a specifier extension (i.e unadorned %p) are
0078 hashed to prevent leaking information about the kernel memory layout. This
0079 has the added benefit of providing a unique identifier. On 64-bit machines
0080 the first 32 bits are zeroed. The kernel will print ``(ptrval)`` until it
0081 gathers enough entropy.
0082 
0083 When possible, use specialised modifiers such as %pS or %pB (described below)
0084 to avoid the need of providing an unhashed address that has to be interpreted
0085 post-hoc. If not possible, and the aim of printing the address is to provide
0086 more information for debugging, use %p and boot the kernel with the
0087 ``no_hash_pointers`` parameter during debugging, which will print all %p
0088 addresses unmodified. If you *really* always want the unmodified address, see
0089 %px below.
0090 
0091 If (and only if) you are printing addresses as a content of a virtual file in
0092 e.g. procfs or sysfs (using e.g. seq_printf(), not printk()) read by a
0093 userspace process, use the %pK modifier described below instead of %p or %px.
0094 
0095 Error Pointers
0096 --------------
0097 
0098 ::
0099 
0100         %pe     -ENOSPC
0101 
0102 For printing error pointers (i.e. a pointer for which IS_ERR() is true)
0103 as a symbolic error name. Error values for which no symbolic name is
0104 known are printed in decimal, while a non-ERR_PTR passed as the
0105 argument to %pe gets treated as ordinary %p.
0106 
0107 Symbols/Function Pointers
0108 -------------------------
0109 
0110 ::
0111 
0112         %pS     versatile_init+0x0/0x110
0113         %ps     versatile_init
0114         %pSR    versatile_init+0x9/0x110
0115                 (with __builtin_extract_return_addr() translation)
0116         %pB     prev_fn_of_versatile_init+0x88/0x88
0117 
0118 
0119 The ``S`` and ``s`` specifiers are used for printing a pointer in symbolic
0120 format. They result in the symbol name with (S) or without (s)
0121 offsets. If KALLSYMS are disabled then the symbol address is printed instead.
0122 
0123 The ``B`` specifier results in the symbol name with offsets and should be
0124 used when printing stack backtraces. The specifier takes into
0125 consideration the effect of compiler optimisations which may occur
0126 when tail-calls are used and marked with the noreturn GCC attribute.
0127 
0128 If the pointer is within a module, the module name and optionally build ID is
0129 printed after the symbol name with an extra ``b`` appended to the end of the
0130 specifier.
0131 
0132 ::
0133 
0134         %pS     versatile_init+0x0/0x110 [module_name]
0135         %pSb    versatile_init+0x0/0x110 [module_name ed5019fdf5e53be37cb1ba7899292d7e143b259e]
0136         %pSRb   versatile_init+0x9/0x110 [module_name ed5019fdf5e53be37cb1ba7899292d7e143b259e]
0137                 (with __builtin_extract_return_addr() translation)
0138         %pBb    prev_fn_of_versatile_init+0x88/0x88 [module_name ed5019fdf5e53be37cb1ba7899292d7e143b259e]
0139 
0140 Probed Pointers from BPF / tracing
0141 ----------------------------------
0142 
0143 ::
0144 
0145         %pks    kernel string
0146         %pus    user string
0147 
0148 The ``k`` and ``u`` specifiers are used for printing prior probed memory from
0149 either kernel memory (k) or user memory (u). The subsequent ``s`` specifier
0150 results in printing a string. For direct use in regular vsnprintf() the (k)
0151 and (u) annotation is ignored, however, when used out of BPF's bpf_trace_printk(),
0152 for example, it reads the memory it is pointing to without faulting.
0153 
0154 Kernel Pointers
0155 ---------------
0156 
0157 ::
0158 
0159         %pK     01234567 or 0123456789abcdef
0160 
0161 For printing kernel pointers which should be hidden from unprivileged
0162 users. The behaviour of %pK depends on the kptr_restrict sysctl - see
0163 Documentation/admin-guide/sysctl/kernel.rst for more details.
0164 
0165 This modifier is *only* intended when producing content of a file read by
0166 userspace from e.g. procfs or sysfs, not for dmesg. Please refer to the
0167 section about %p above for discussion about how to manage hashing pointers
0168 in printk().
0169 
0170 Unmodified Addresses
0171 --------------------
0172 
0173 ::
0174 
0175         %px     01234567 or 0123456789abcdef
0176 
0177 For printing pointers when you *really* want to print the address. Please
0178 consider whether or not you are leaking sensitive information about the
0179 kernel memory layout before printing pointers with %px. %px is functionally
0180 equivalent to %lx (or %lu). %px is preferred because it is more uniquely
0181 grep'able. If in the future we need to modify the way the kernel handles
0182 printing pointers we will be better equipped to find the call sites.
0183 
0184 Before using %px, consider if using %p is sufficient together with enabling the
0185 ``no_hash_pointers`` kernel parameter during debugging sessions (see the %p
0186 description above). One valid scenario for %px might be printing information
0187 immediately before a panic, which prevents any sensitive information to be
0188 exploited anyway, and with %px there would be no need to reproduce the panic
0189 with no_hash_pointers.
0190 
0191 Pointer Differences
0192 -------------------
0193 
0194 ::
0195 
0196         %td     2560
0197         %tx     a00
0198 
0199 For printing the pointer differences, use the %t modifier for ptrdiff_t.
0200 
0201 Example::
0202 
0203         printk("test: difference between pointers: %td\n", ptr2 - ptr1);
0204 
0205 Struct Resources
0206 ----------------
0207 
0208 ::
0209 
0210         %pr     [mem 0x60000000-0x6fffffff flags 0x2200] or
0211                 [mem 0x0000000060000000-0x000000006fffffff flags 0x2200]
0212         %pR     [mem 0x60000000-0x6fffffff pref] or
0213                 [mem 0x0000000060000000-0x000000006fffffff pref]
0214 
0215 For printing struct resources. The ``R`` and ``r`` specifiers result in a
0216 printed resource with (R) or without (r) a decoded flags member.
0217 
0218 Passed by reference.
0219 
0220 Physical address types phys_addr_t
0221 ----------------------------------
0222 
0223 ::
0224 
0225         %pa[p]  0x01234567 or 0x0123456789abcdef
0226 
0227 For printing a phys_addr_t type (and its derivatives, such as
0228 resource_size_t) which can vary based on build options, regardless of the
0229 width of the CPU data path.
0230 
0231 Passed by reference.
0232 
0233 DMA address types dma_addr_t
0234 ----------------------------
0235 
0236 ::
0237 
0238         %pad    0x01234567 or 0x0123456789abcdef
0239 
0240 For printing a dma_addr_t type which can vary based on build options,
0241 regardless of the width of the CPU data path.
0242 
0243 Passed by reference.
0244 
0245 Raw buffer as an escaped string
0246 -------------------------------
0247 
0248 ::
0249 
0250         %*pE[achnops]
0251 
0252 For printing raw buffer as an escaped string. For the following buffer::
0253 
0254                 1b 62 20 5c 43 07 22 90 0d 5d
0255 
0256 A few examples show how the conversion would be done (excluding surrounding
0257 quotes)::
0258 
0259                 %*pE            "\eb \C\a"\220\r]"
0260                 %*pEhp          "\x1bb \C\x07"\x90\x0d]"
0261                 %*pEa           "\e\142\040\\\103\a\042\220\r\135"
0262 
0263 The conversion rules are applied according to an optional combination
0264 of flags (see :c:func:`string_escape_mem` kernel documentation for the
0265 details):
0266 
0267         - a - ESCAPE_ANY
0268         - c - ESCAPE_SPECIAL
0269         - h - ESCAPE_HEX
0270         - n - ESCAPE_NULL
0271         - o - ESCAPE_OCTAL
0272         - p - ESCAPE_NP
0273         - s - ESCAPE_SPACE
0274 
0275 By default ESCAPE_ANY_NP is used.
0276 
0277 ESCAPE_ANY_NP is the sane choice for many cases, in particularly for
0278 printing SSIDs.
0279 
0280 If field width is omitted then 1 byte only will be escaped.
0281 
0282 Raw buffer as a hex string
0283 --------------------------
0284 
0285 ::
0286 
0287         %*ph    00 01 02  ...  3f
0288         %*phC   00:01:02: ... :3f
0289         %*phD   00-01-02- ... -3f
0290         %*phN   000102 ... 3f
0291 
0292 For printing small buffers (up to 64 bytes long) as a hex string with a
0293 certain separator. For larger buffers consider using
0294 :c:func:`print_hex_dump`.
0295 
0296 MAC/FDDI addresses
0297 ------------------
0298 
0299 ::
0300 
0301         %pM     00:01:02:03:04:05
0302         %pMR    05:04:03:02:01:00
0303         %pMF    00-01-02-03-04-05
0304         %pm     000102030405
0305         %pmR    050403020100
0306 
0307 For printing 6-byte MAC/FDDI addresses in hex notation. The ``M`` and ``m``
0308 specifiers result in a printed address with (M) or without (m) byte
0309 separators. The default byte separator is the colon (:).
0310 
0311 Where FDDI addresses are concerned the ``F`` specifier can be used after
0312 the ``M`` specifier to use dash (-) separators instead of the default
0313 separator.
0314 
0315 For Bluetooth addresses the ``R`` specifier shall be used after the ``M``
0316 specifier to use reversed byte order suitable for visual interpretation
0317 of Bluetooth addresses which are in the little endian order.
0318 
0319 Passed by reference.
0320 
0321 IPv4 addresses
0322 --------------
0323 
0324 ::
0325 
0326         %pI4    1.2.3.4
0327         %pi4    001.002.003.004
0328         %p[Ii]4[hnbl]
0329 
0330 For printing IPv4 dot-separated decimal addresses. The ``I4`` and ``i4``
0331 specifiers result in a printed address with (i4) or without (I4) leading
0332 zeros.
0333 
0334 The additional ``h``, ``n``, ``b``, and ``l`` specifiers are used to specify
0335 host, network, big or little endian order addresses respectively. Where
0336 no specifier is provided the default network/big endian order is used.
0337 
0338 Passed by reference.
0339 
0340 IPv6 addresses
0341 --------------
0342 
0343 ::
0344 
0345         %pI6    0001:0002:0003:0004:0005:0006:0007:0008
0346         %pi6    00010002000300040005000600070008
0347         %pI6c   1:2:3:4:5:6:7:8
0348 
0349 For printing IPv6 network-order 16-bit hex addresses. The ``I6`` and ``i6``
0350 specifiers result in a printed address with (I6) or without (i6)
0351 colon-separators. Leading zeros are always used.
0352 
0353 The additional ``c`` specifier can be used with the ``I`` specifier to
0354 print a compressed IPv6 address as described by
0355 https://tools.ietf.org/html/rfc5952
0356 
0357 Passed by reference.
0358 
0359 IPv4/IPv6 addresses (generic, with port, flowinfo, scope)
0360 ---------------------------------------------------------
0361 
0362 ::
0363 
0364         %pIS    1.2.3.4         or 0001:0002:0003:0004:0005:0006:0007:0008
0365         %piS    001.002.003.004 or 00010002000300040005000600070008
0366         %pISc   1.2.3.4         or 1:2:3:4:5:6:7:8
0367         %pISpc  1.2.3.4:12345   or [1:2:3:4:5:6:7:8]:12345
0368         %p[Ii]S[pfschnbl]
0369 
0370 For printing an IP address without the need to distinguish whether it's of
0371 type AF_INET or AF_INET6. A pointer to a valid struct sockaddr,
0372 specified through ``IS`` or ``iS``, can be passed to this format specifier.
0373 
0374 The additional ``p``, ``f``, and ``s`` specifiers are used to specify port
0375 (IPv4, IPv6), flowinfo (IPv6) and scope (IPv6). Ports have a ``:`` prefix,
0376 flowinfo a ``/`` and scope a ``%``, each followed by the actual value.
0377 
0378 In case of an IPv6 address the compressed IPv6 address as described by
0379 https://tools.ietf.org/html/rfc5952 is being used if the additional
0380 specifier ``c`` is given. The IPv6 address is surrounded by ``[``, ``]`` in
0381 case of additional specifiers ``p``, ``f`` or ``s`` as suggested by
0382 https://tools.ietf.org/html/draft-ietf-6man-text-addr-representation-07
0383 
0384 In case of IPv4 addresses, the additional ``h``, ``n``, ``b``, and ``l``
0385 specifiers can be used as well and are ignored in case of an IPv6
0386 address.
0387 
0388 Passed by reference.
0389 
0390 Further examples::
0391 
0392         %pISfc          1.2.3.4         or [1:2:3:4:5:6:7:8]/123456789
0393         %pISsc          1.2.3.4         or [1:2:3:4:5:6:7:8]%1234567890
0394         %pISpfc         1.2.3.4:12345   or [1:2:3:4:5:6:7:8]:12345/123456789
0395 
0396 UUID/GUID addresses
0397 -------------------
0398 
0399 ::
0400 
0401         %pUb    00010203-0405-0607-0809-0a0b0c0d0e0f
0402         %pUB    00010203-0405-0607-0809-0A0B0C0D0E0F
0403         %pUl    03020100-0504-0706-0809-0a0b0c0e0e0f
0404         %pUL    03020100-0504-0706-0809-0A0B0C0E0E0F
0405 
0406 For printing 16-byte UUID/GUIDs addresses. The additional ``l``, ``L``,
0407 ``b`` and ``B`` specifiers are used to specify a little endian order in
0408 lower (l) or upper case (L) hex notation - and big endian order in lower (b)
0409 or upper case (B) hex notation.
0410 
0411 Where no additional specifiers are used the default big endian
0412 order with lower case hex notation will be printed.
0413 
0414 Passed by reference.
0415 
0416 dentry names
0417 ------------
0418 
0419 ::
0420 
0421         %pd{,2,3,4}
0422         %pD{,2,3,4}
0423 
0424 For printing dentry name; if we race with :c:func:`d_move`, the name might
0425 be a mix of old and new ones, but it won't oops.  %pd dentry is a safer
0426 equivalent of %s dentry->d_name.name we used to use, %pd<n> prints ``n``
0427 last components.  %pD does the same thing for struct file.
0428 
0429 Passed by reference.
0430 
0431 block_device names
0432 ------------------
0433 
0434 ::
0435 
0436         %pg     sda, sda1 or loop0p1
0437 
0438 For printing name of block_device pointers.
0439 
0440 struct va_format
0441 ----------------
0442 
0443 ::
0444 
0445         %pV
0446 
0447 For printing struct va_format structures. These contain a format string
0448 and va_list as follows::
0449 
0450         struct va_format {
0451                 const char *fmt;
0452                 va_list *va;
0453         };
0454 
0455 Implements a "recursive vsnprintf".
0456 
0457 Do not use this feature without some mechanism to verify the
0458 correctness of the format string and va_list arguments.
0459 
0460 Passed by reference.
0461 
0462 Device tree nodes
0463 -----------------
0464 
0465 ::
0466 
0467         %pOF[fnpPcCF]
0468 
0469 
0470 For printing device tree node structures. Default behaviour is
0471 equivalent to %pOFf.
0472 
0473         - f - device node full_name
0474         - n - device node name
0475         - p - device node phandle
0476         - P - device node path spec (name + @unit)
0477         - F - device node flags
0478         - c - major compatible string
0479         - C - full compatible string
0480 
0481 The separator when using multiple arguments is ':'
0482 
0483 Examples::
0484 
0485         %pOF    /foo/bar@0                      - Node full name
0486         %pOFf   /foo/bar@0                      - Same as above
0487         %pOFfp  /foo/bar@0:10                   - Node full name + phandle
0488         %pOFfcF /foo/bar@0:foo,device:--P-      - Node full name +
0489                                                   major compatible string +
0490                                                   node flags
0491                                                         D - dynamic
0492                                                         d - detached
0493                                                         P - Populated
0494                                                         B - Populated bus
0495 
0496 Passed by reference.
0497 
0498 Fwnode handles
0499 --------------
0500 
0501 ::
0502 
0503         %pfw[fP]
0504 
0505 For printing information on fwnode handles. The default is to print the full
0506 node name, including the path. The modifiers are functionally equivalent to
0507 %pOF above.
0508 
0509         - f - full name of the node, including the path
0510         - P - the name of the node including an address (if there is one)
0511 
0512 Examples (ACPI)::
0513 
0514         %pfwf   \_SB.PCI0.CIO2.port@1.endpoint@0        - Full node name
0515         %pfwP   endpoint@0                              - Node name
0516 
0517 Examples (OF)::
0518 
0519         %pfwf   /ocp@68000000/i2c@48072000/camera@10/port/endpoint - Full name
0520         %pfwP   endpoint                                - Node name
0521 
0522 Time and date
0523 -------------
0524 
0525 ::
0526 
0527         %pt[RT]                 YYYY-mm-ddTHH:MM:SS
0528         %pt[RT]s                YYYY-mm-dd HH:MM:SS
0529         %pt[RT]d                YYYY-mm-dd
0530         %pt[RT]t                HH:MM:SS
0531         %pt[RT][dt][r][s]
0532 
0533 For printing date and time as represented by::
0534 
0535         R  struct rtc_time structure
0536         T  time64_t type
0537 
0538 in human readable format.
0539 
0540 By default year will be incremented by 1900 and month by 1.
0541 Use %pt[RT]r (raw) to suppress this behaviour.
0542 
0543 The %pt[RT]s (space) will override ISO 8601 separator by using ' ' (space)
0544 instead of 'T' (Capital T) between date and time. It won't have any effect
0545 when date or time is omitted.
0546 
0547 Passed by reference.
0548 
0549 struct clk
0550 ----------
0551 
0552 ::
0553 
0554         %pC     pll1
0555         %pCn    pll1
0556 
0557 For printing struct clk structures. %pC and %pCn print the name of the clock
0558 (Common Clock Framework) or a unique 32-bit ID (legacy clock framework).
0559 
0560 Passed by reference.
0561 
0562 bitmap and its derivatives such as cpumask and nodemask
0563 -------------------------------------------------------
0564 
0565 ::
0566 
0567         %*pb    0779
0568         %*pbl   0,3-6,8-10
0569 
0570 For printing bitmap and its derivatives such as cpumask and nodemask,
0571 %*pb outputs the bitmap with field width as the number of bits and %*pbl
0572 output the bitmap as range list with field width as the number of bits.
0573 
0574 The field width is passed by value, the bitmap is passed by reference.
0575 Helper macros cpumask_pr_args() and nodemask_pr_args() are available to ease
0576 printing cpumask and nodemask.
0577 
0578 Flags bitfields such as page flags, gfp_flags
0579 ---------------------------------------------
0580 
0581 ::
0582 
0583         %pGp    0x17ffffc0002036(referenced|uptodate|lru|active|private|node=0|zone=2|lastcpupid=0x1fffff)
0584         %pGg    GFP_USER|GFP_DMA32|GFP_NOWARN
0585         %pGv    read|exec|mayread|maywrite|mayexec|denywrite
0586 
0587 For printing flags bitfields as a collection of symbolic constants that
0588 would construct the value. The type of flags is given by the third
0589 character. Currently supported are [p]age flags, [v]ma_flags (both
0590 expect ``unsigned long *``) and [g]fp_flags (expects ``gfp_t *``). The flag
0591 names and print order depends on the particular type.
0592 
0593 Note that this format should not be used directly in the
0594 :c:func:`TP_printk()` part of a tracepoint. Instead, use the show_*_flags()
0595 functions from <trace/events/mmflags.h>.
0596 
0597 Passed by reference.
0598 
0599 Network device features
0600 -----------------------
0601 
0602 ::
0603 
0604         %pNF    0x000000000000c000
0605 
0606 For printing netdev_features_t.
0607 
0608 Passed by reference.
0609 
0610 V4L2 and DRM FourCC code (pixel format)
0611 ---------------------------------------
0612 
0613 ::
0614 
0615         %p4cc
0616 
0617 Print a FourCC code used by V4L2 or DRM, including format endianness and
0618 its numerical value as hexadecimal.
0619 
0620 Passed by reference.
0621 
0622 Examples::
0623 
0624         %p4cc   BG12 little-endian (0x32314742)
0625         %p4cc   Y10  little-endian (0x20303159)
0626         %p4cc   NV12 big-endian (0xb231564e)
0627 
0628 Thanks
0629 ======
0630 
0631 If you add other %p extensions, please extend <lib/test_printf.c> with
0632 one or more test cases, if at all feasible.
0633 
0634 Thank you for your cooperation and attention.