PRINTF(9)
β¨ PRINTF(9) β the kernel dev's manual, bestie edition β¨
okay so. you opened the PRINTF(9) page from the Kernel Developer's Manual and you're like "why is there a ninth section" and honestly valid question, but buckle up because this family of functions is lowkey iconic and you're gonna use it constantly. π
πΈ NAME πΈ
printf, uprintf, tprintf, log β formatted output conversion π«Ά
β¨ SYNOPSIS β¨
bestie, here's the whole cast, character for character, no edits:
#include <sys/types.h>
#include <sys/systm.h>
int
printf(const char *fmt, ...);
void
tprintf(struct proc *p, int pri, const char *fmt, ...);
int
uprintf(const char *fmt, ...);
int
vprintf(const char *fmt, va_list ap);
#include <sys/syslog.h>
void
log(int pri, const char *fmt, ...);
void
vlog(int pri, const char *fmt, va_list ap);
π DESCRIPTION π
so the printf(9) family is giving extremely printf(3) energy β same vibes, same formatting brain β except every function in the group sends its output somewhere different. that's the whole plot twist. π
uprintf()β drops its output onto the controlling tty of the current process.printf()β writes to the console and to the logging facility. two places. multitasking queen. πtprintf()β writes to the tty associated with the processp, and also to the logging facility β but only ifpriis not-1.log()β sends the message to the kernel logging facility, using the log level given bypri, and also to the console if no process is yet reading the log.
Every one of these related functions uses the fmt parameter exactly the same way printf(3) does. no notes. but!! β¨ printf(9) adds two extra conversion specifiers and this is the part where it gets good:
π the %b identifier π
%b expects two arguments: an int and a char *. they get used as a register value and a print mask for decoding bitmasks. the print mask is built from two parts: the base and the arguments.
- the base value is the output base written as an integer value. for example,
\10gives octal and\20gives hexadecimal. - the arguments are a sequence of bit identifiers. each bit identifier starts with an integer value β that's the number of the bit (starting from 1) that this identifier describes. the rest of the identifier is a string of characters containing the name of the bit. that string is terminated either by the bit number at the start of the next bit identifier, or by NUL for the last bit identifier.
π¦ the %D identifier π¦
%D is here to help with hexdumps. it requires two arguments: a u_char * pointer and a char * string.
- the memory the pointer points at gets output in hexadecimal, one byte at a time.
- the string is used as a delimiter between individual bytes.
- if present, a width directive specifies the number of bytes to display.
- by default, 16 bytes of data are output. no cap. π
π about log()'s pri π
log() uses syslog(3) level values LOG_DEBUG through LOG_EMERG for its pri parameter β which the manpage notes is mistakenly called βpriorityβ here. π
alternatively, if you hand it a pri of -1, the message gets appended to the last log message started by a previous call to log(). and because these messages are generated by the kernel itself, the facility will always be LOG_KERN. periodt.
β RETURN VALUES β
the printf() and the uprintf() functions return the number of characters displayed. that's it, that's the tea. π
π₯ EXAMPLES π₯
this example shows off both the %b and %D conversion specifiers. the function:
void
printf_test(void)
{
printf("reg=%b\n", 3, "\10\2BITTWO\1BITONE");
printf("out: %4D\n", "AAAA", ":");
}
will produce the following output:
reg=3<BITTWO,BITONE>
out: 41:41:41:41
ate. πβ¨
and the call:
log(LOG_DEBUG, "%s%d: been there.\n", sc->sc_name, sc->sc_unit);
will add the appropriate debug message at priority βkern.debugβ to the system log. main character log entry fr fr. π
π SEE ALSO π
and that's the whole page bestie, go forth and format π«Ά this one came from the Debian edition, dated November 18, 2015 β so it's vintage but she still slays. πβ¨π
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 2001 Andrew R. Reiter
- Copyright (c) 2004 Joerg Wunsch
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms