Skip to contentSkip to conversation

docs: printk-formats: Clarify %*pb format parameters

PATCH In review Luca Weiss 5 participants 6 messages May 16, 2023 – May 24, 2023

Subsystems (2)
lore
Share & tools
lore

Luca Weiss

author
#0 lore raw

PATCH 1/1 docs: printk-formats: Clarify %*pb format parameters

Since it's rather unusual for printk formats to require two parameters, expand the documentation to clearly mention that in the printk format for bitmaps.

As an extra example, for example from include/net/bluetooth/hci_core.h we have a bitmap DECLARE_BITMAP(dev_flags, __HCI_NUM_FLAGS); This can be printed with ("%*pb", __HCI_NUM_FLAGS, hdev->dev_flags)

---
This probably also applies to other printk formats but since this is the only one so far I've used that requires two parameters I cannot really help with the others.

Someone with more knowledge on the other more unusual format strings could apply this to others also.

---
 Documentation/core-api/printk-formats.rst | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
Documentation/core-api/printk-formats.rst +3 -1
index dfe7e75a71de..3cafb988d757 100644
@@ -571,7 +571,9 @@ For printing bitmap and its derivatives such as cpumask and nodemask,
 %*pb outputs the bitmap with field width as the number of bits and %*pbl
 output the bitmap as range list with field width as the number of bits.
 
-The field width is passed by value, the bitmap is passed by reference.
+This requires two parameters, the field width (number of bits in the bitmap)
+passed by value and the bitmap passed by reference.
+
 Helper macros cpumask_pr_args() and nodemask_pr_args() are available to ease
 printing cpumask and nodemask.
 

---
base-commit: 16a8829130ca22666ac6236178a6233208d425c3
change-id: 20230516-printk-bitmap-bcffd77379ac

Best regards,

Signature · 1 line
Luca Weiss <[email protected]>
#1 lore raw
Quoted 1 line · Luca Weiss

Since it's rather unusual for printk formats to require two parameters,

I don't think this is "unusual". The %*... is very well described in the printf(3) (the meaning of *). It applies here for %*p... as well.

Quoted 6 lines

expand the documentation to clearly mention that in the printk format for bitmaps.

As an extra example, for example from include/net/bluetooth/hci_core.h we have a bitmap DECLARE_BITMAP(dev_flags, __HCI_NUM_FLAGS); This can be printed with ("%*pb", __HCI_NUM_FLAGS, hdev->dev_flags)

I think this change is not needed. But try to convince PRINTK maintainers. Maybe they will be okay with it if it covers all %*p... cases (like %*ph).

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author
#2 lore raw
Quoted 4 lines · Andy Shevchenko
Quoted 1 line · Luca Weiss

Since it's rather unusual for printk formats to require two parameters,

I don't think this is "unusual".

Well, unusual for me at least. I've never really used anything except the standard integer types and %px. And I've been contributing to the kernel since 2017.

Quoted 2 lines

The %*... is very well described in the printf(3) (the meaning of *). It applies here for %*p... as well.

The printf(3) man page is never mentioned on this docs page. And even if it would be mentioned somewhere, I doubt anyone is reading most of the page when they're just looking for a format specifier for e.g. bitmap. At least I am not.

Quoted 10 lines
Quoted 7 lines

expand the documentation to clearly mention that in the printk format for bitmaps.

As an extra example, for example from include/net/bluetooth/hci_core.h we have a bitmap DECLARE_BITMAP(dev_flags, __HCI_NUM_FLAGS); This can be printed with ("%*pb", __HCI_NUM_FLAGS, hdev->dev_flags)

I think this change is not needed. But try to convince PRINTK maintainers. Maybe they will be okay with it if it covers all %*p... cases (like %*ph).

Cannot say I haven't tried :) As I wrote in the initial email I'd be happy also if someone who knows more about the formats to include some text for all the %* options so users know what's up.

Regards
Luca

maintainer
#3 lore raw

PATCH 1/1 vsprintf/doc: Document format flags including field width and precision

The kernel implementation of vsprintf() tries to be as compatible with the user space variant as possible. Though it does not implement all features. On the other hand, it adds some special pointer printing modifiers.

Most differences are described in Documentation/core-api/printk-formats.rst Add the missing documentation of the supported flag characters '#', '0', '-', ' ', '+' together with field width and precision modifiers.

---
What about something like this, please?

 Documentation/core-api/printk-formats.rst | 69 +++++++++++++++++++++++
 1 file changed, 69 insertions(+)
Documentation/core-api/printk-formats.rst +69 -0
index dfe7e75a71de..79655b319658 100644
@@ -8,6 +8,75 @@ How to get printk format specifiers right
 :Author: Andrew Murray <[email protected]>
 
 
+Flag characters
+===============
+
+The character '%' might be followed by the following flags that modify
+the output:
+
+	- '#' - prepend '0', '0x', or 'OX for 'o', 'x', 'X' number conversions
+	- '0' - zero pad number conversions on the field boundary
+	- '-' - left adjust on the field boundary, blank pad on the right
+	- ' ' - prepend space on positive numbers
+	- '+' - prepend + for positive numbers when using signed formats
+
+Examples::
+
+	|%x|	|1a|
+	|%#x|	|0x1a|
+	|%d|	|26|
+	|% d|	| 26|
+	|%+d|	|+26|
+
+
+Field width
+===========
+
+A field width may be defined when '%' is optionally followed by the above flag
+characters and:
+
+	- 'number' - the decimal number defines the field width
+	- '*' the field width is defined by an extra parameter
+
+Values are never truncated when the filed width is not big enough.
+Spaces are used by default when a padding is needed.
+
+Examples::
+
+	|%6d|	|    26|
+	|%-6d|	|26    |
+	|%06d|	|000026|
+
+	printk("Dynamic table: |%*d|%*s|\n", id_width, id, max_name_len, name);
+
+The filed width value might have special meaning for some pointer formats.
+For example, it limits the size of the bitmap handled by %*pb format.
+
+
+
+Field precision:
+================
+
+A field width may be defined when '%' is optionally followed by the above flag
+characters:
+
+	- '.number' - the decimal number defines the field precision
+	- '.*' the field precision is defined by an extra parameter
+
+The precision defines:
+
+	- number of digits after the decimal point in float number conversions
+	- minimal number of digits in integer conversions
+	- maximum number of characters in string conversions
+
+Examples::
+
+	|%.3f|	|12.300|
+	|%.6d|	|    26|
+	|%.6s|	|Hi, th|
+
+	printk("Dynamic precision: %.*f\n", precision, value);
+
 Integer types
 =============
 
Signature · 1 line
2.35.3
#4 lore raw

Re: [PATCH] vsprintf/doc: Document format flags including field width and precision

Quoted 26 lines · Petr Mladek

The kernel implementation of vsprintf() tries to be as compatible with the user space variant as possible. Though it does not implement all features. On the other hand, it adds some special pointer printing modifiers.

Most differences are described in Documentation/core-api/printk-formats.rst Add the missing documentation of the supported flag characters '#', '0', '-', ' ', '+' together with field width and precision modifiers.

---
What about something like this, please?

 Documentation/core-api/printk-formats.rst | 69 +++++++++++++++++++++++
 1 file changed, 69 insertions(+)
Documentation/core-api/printk-formats.rst +1 -0
index dfe7e75a71de..79655b319658 100644
@@ -8,6 +8,75 @@ How to get printk format specifiers right
 :Author: Andrew Murray <[email protected]>
 
 
+Flag characters

Documentation/core-api/printk-formats.rst How to get printk format specifiers right · line 21

Quoted 10 lines
Documentation/core-api/printk-formats.rst +10 -0
+===============
+
+The character '%' might be followed by the following flags that modify
+the output:
+
+	- '#' - prepend '0', '0x', or 'OX for 'o', 'x', 'X' number conversions
+	- '0' - zero pad number conversions on the field boundary
+	- '-' - left adjust on the field boundary, blank pad on the right
+	- ' ' - prepend space on positive numbers
+	- '+' - prepend + for positive numbers when using signed formats

[I wonder if we have a single user of any of the latter two in the entire tree.]

Quoted 9 lines
Documentation/core-api/printk-formats.rst +9 -0
+Examples::
+
+	|%x|	|1a|
+	|%#x|	|0x1a|
+	|%d|	|26|
+	|% d|	| 26|
+	|%+d|	|+26|
+
+

Documentation/core-api/printk-formats.rst How to get printk format specifiers right · line 41

Quoted 10 lines
Documentation/core-api/printk-formats.rst +10 -0
+Field width
+===========
+
+A field width may be defined when '%' is optionally followed by the above flag
+characters and:
+
+	- 'number' - the decimal number defines the field width
+	- '*' the field width is defined by an extra parameter
+
+Values are never truncated when the filed width is not big enough.

filed -> field (several places)

Documentation/core-api/printk-formats.rst How to get printk format specifiers right · line 53

Quoted 12 lines
Documentation/core-api/printk-formats.rst +12 -0
+Spaces are used by default when a padding is needed.
+
+Examples::
+
+	|%6d|	|    26|
+	|%-6d|	|26    |
+	|%06d|	|000026|
+
+	printk("Dynamic table: |%*d|%*s|\n", id_width, id, max_name_len, name);
+
+The filed width value might have special meaning for some pointer formats.
+For example, it limits the size of the bitmap handled by %*pb format.

It should also be noted that a negative field width passed as a * argument is interpreted as if the - flag is used and then the absolute value is used as field width.

Quoted 4 lines
Documentation/core-api/printk-formats.rst +4 -0
+
+
+Field precision:
+================

Documentation/core-api/printk-formats.rst How to get printk format specifiers right · line 68

Quoted 10 lines
Documentation/core-api/printk-formats.rst +10 -0
+
+A field width may be defined when '%' is optionally followed by the above flag
+characters:
+
+	- '.number' - the decimal number defines the field precision
+	- '.*' the field precision is defined by an extra parameter
+
+The precision defines:
+
+	- number of digits after the decimal point in float number conversions

No, don't mention floats, the kernel doesn't do those.

Documentation/core-api/printk-formats.rst How to get printk format specifiers right · line 74

Quoted 6 lines
Documentation/core-api/printk-formats.rst +6 -0
+	- minimal number of digits in integer conversions
+	- maximum number of characters in string conversions
+
+Examples::
+
+	|%.3f|	|12.300|

Remove.

Documentation/core-api/printk-formats.rst How to get printk format specifiers right · line 75

Quoted 1 line
Documentation/core-api/printk-formats.rst +1 -0
+	|%.6d|	|    26|

Nope, that actually produces 000026.

---

So overall, I'm not sure this is a net win. I think it might be better to emphasize that

- the kernel doesn't do floats, argument reordering via m$, wide characters/strings, %m or %n (just so that's out of the equation)

- for string and integer conversions, the kernel's printf is very very close to following POSIX/libc/whatever, in terms of flags, field width etc. [There are a few exceptions, those I've found are documented in test_printf.c, but nobody is ever likely to hit those.]

- for %p, the kernel has its own rules, starting with the fact that modifying behaviour based on alphanumerics following the p is completely non-standard.

and then spend the rest explaining those rules, and perhaps also some background on why the %p extensions exist and why they are implemented the way they are - for example "we want -Wformat to tell us if something is wrong", but that, for example, means we can only use a field width and not a precision to pass an extra argument to a %psomething. And alphanumerics are chosen because nobody would usually follow a normal %p by anything but whitespace or punctuation, and because the compiler format checking is happy as long as there's some pointer argument corresponding to the %p, and the remaining characters are, from the compiler's POV, just literal characters.

Rasmus

#5 lore raw

Re: [PATCH] vsprintf/doc: Document format flags including field width and precision

In reply to Petr Mladek

Documentation/core-api/printk-formats.rst How to get printk format specifiers right

Quoted 5 lines · Petr Mladek
Documentation/core-api/printk-formats.rst +3 -0
+Examples::
 
+	printk("Dynamic table: |%*d|%*s|\n", id_width, id, max_name_len, name);
 
+	printk("Dynamic precision: %.*f\n", precision, value);

Generally speaking, it is rare to use printk() without a loglevel specified. And it is preferred that the pr_ or dev_ macros are used. Perhaps the examples should call pr_info() instead?

John Ogness