diff options
author | Kent Overstreet <kent.overstreet@gmail.com> | 2022-04-25 13:09:13 -0400 |
---|---|---|
committer | Kent Overstreet <kent.overstreet@gmail.com> | 2022-05-20 13:54:43 -0400 |
commit | 06559f32b421d6a02b070781faa1c4e149a1fcbe (patch) | |
tree | 7bb5fd1707b363a67be6847aef673393a203164e /lib/test_printf.c | |
parent | 6ddbd4f93c9526e50c71d98914f9395134325968 (diff) |
vsprintf: %pf(%p)
This implements two new format strings: both do the same thing, one more
compatible with current gcc format string checking, the other that we'd
like to standardize:
%pf(%p) - more compatible
%(%p) - more prettier
Both can take variable numbers of arguments, i.e. %(%p,%p,%p).
They're used to indicate that snprintf or pr_buf should interpret the
next argument as a pretty-printer function to call, and subsequent
arguments within the parentheses should be passed to the pretty-printer.
A pretty printer takes as its first argument a printbuf, and then zero
or more pointer arguments - integer arguments are not (currently) supported.
Example usage:
static void foo_to_text(struct printbuf *out, struct foo *foo)
{
pr_buf(out, "bar=%u baz=%u", foo->bar, foo->baz);
}
printf("%(%p)", foo_to_text, foo);
The goal is to replace most of our %p format extensions with this
interface, and to move pretty-printers out of the core vsprintf.c code -
this will get us better organization and better discoverability (you'll
be able to cscope to pretty printer calls!), as well as eliminate a lot
of dispatch code in vsprintf.c.
Currently, we can only call pretty printers with pointer arguments. This
could be changed to also allow at least integer arguments in the future
by using libffi.
Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
Reviewed-by: Matthew Wilcox (Oracle) <willy@infradead.org>
Diffstat (limited to 'lib/test_printf.c')
-rw-r--r-- | lib/test_printf.c | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/lib/test_printf.c b/lib/test_printf.c index 07309c45f327..52df049d4ab8 100644 --- a/lib/test_printf.c +++ b/lib/test_printf.c @@ -783,6 +783,25 @@ test_pointer(void) fourcc_pointer(); } +static void printf_test_fn(struct printbuf *out, void *p) +{ + int *i = p; + + pr_buf(out, "%i", *i); +} + +static void __init +test_fn(void) +{ + int i = 1; + + test("1", "%pf(%p)", printf_test_fn, &i); + /* + * Not tested, so we don't fail the build with -Werror: + */ + //test("1", "%(%p)", printf_test_fn, &i); +} + static void __init selftest(void) { alloced_buffer = kmalloc(BUF_SIZE + 2*PAD_SIZE, GFP_KERNEL); @@ -794,6 +813,7 @@ static void __init selftest(void) test_number(); test_string(); test_pointer(); + test_fn(); kfree(alloced_buffer); } |