KPRINTF(9)
π KPRINTF(9) β kernel formatted output conversion, bestie edition π
okay so this is the KPRINTF(9) page from the Kernel Developer's Manual, and it's literally the entire lore of how the kernel prints stuff. formatted output conversion, but make it fashion β¨
πΈ NAME πΈ
device_printf, printf, printf_nolog, printf_nostamp, snprintf, vasprintf, vprintf, vsnprintf, uprintf, ttyprintf, tprintf_open, tprintf, tprintf_close, aprint_normal, aprint_naive, aprint_verbose, aprint_debug, aprint_error, aprint_normal_dev, aprint_naive_dev, aprint_verbose_dev, aprint_debug_dev, aprint_error_dev, aprint_normal_ifnet, aprint_naive_ifnet, aprint_verbose_ifnet, aprint_debug_ifnet, aprint_error_ifnet, aprint_get_error_count β kernel formatted output conversion π«Ά
that's a lot of names, but they're all one big happy family, i promise.
β¨ SYNOPSIS β¨
#include <sys/systm.h>
void
device_printf(device_t, const char *format, ...);
void
printf(const char *format, ...);
void
printf_nolog(const char *format, ...);
void
printf_nostamp(const char *format, ...);
int
snprintf(char *buf, size_t size, const char *format, ...);
int
vasprintf(char **buf, const char *format, va_list ap);
void
vprintf(const char *format, va_list ap);
int
vsnprintf(char *buf, size_t size, const char *format, va_list ap);
void
uprintf(const char *format, ...);
void
ttyprintf(struct tty *tty, const char *format, ...);
then, plot twist, a whole second header β the tprintf girlies get their own clubhouse:
#include <sys/tprintf.h>
tpr_t
tprintf_open(struct proc *p);
void
tprintf(tpr_t tpr, const char *format, ...);
void
tprintf_close(tpr_t tpr);
and now the autoconf roster, in all its glory π
void
aprint_normal(const char *format, ...);
void
aprint_naive(const char *format, ...);
void
aprint_verbose(const char *format, ...);
void
aprint_debug(const char *format, ...);
void
aprint_error(const char *format, ...);
void
aprint_normal_dev(device_t, const char *format, ...);
void
aprint_naive_dev(device_t, const char *format, ...);
void
aprint_verbose_dev(device_t, const char *format, ...);
void
aprint_debug_dev(device_t, const char *format, ...);
void
aprint_error_dev(device_t, const char *format, ...);
void
aprint_normal_ifnet(struct ifnet *, const char *format, ...);
void
aprint_naive_ifnet(struct ifnet *, const char *format, ...);
void
aprint_verbose_ifnet(struct ifnet *, const char *format, ...);
void
aprint_debug_ifnet(struct ifnet *, const char *format, ...);
void
aprint_error_ifnet(struct ifnet *, const char *format, ...);
int
aprint_get_error_count(void);
π DESCRIPTION π
so. the printf() family of functions is how the kernel sends formatted messages out to various output devices. main character energy, truly. π
printf() and vprintf() send formatted strings to the system console. πΊ
device_printf() is identical to printf(), except it prefixes the log message with the corresponding device name. it's giving "hello, it's me, your device" energy.
printf_nolog() is identical to printf(), except it does not send the data to the system log. sneaky.
printf_nostamp() is identical to printf(), except it does not prefix the output with a timestamp. no clock, just vibes.
snprintf(), vasprintf(), and vsnprintf() write output to a string buffer instead. these five functions work similarly to their user space counterparts, and honestly? they're not described in detail here. iykyk.
π«Ά heads up bestie:vasprintf()allocates memory withkmem_alloc(9), and it is the caller's responsibility to free the returned string withkmem_free(9). no cap, don't leak it.
uprintf() and ttyprintf() send formatted strings to the current process's controlling tty and a specific tty, respectively. two different ttys, two different jobs.
tprintf() sends formatted strings to a process's controlling tty, via a handle of type tpr_t. the whole point? it lets you do multiple write operations to the tty with a guarantee that the tty will be valid across calls. iconic, honestly. π
here's the flow: you acquire a handle by calling tprintf_open() with the target process as an argument. this handle must be closed with a matching call to tprintf_close(). matching. call. don't leave it hanging. π
πΌ the aprint girlies
the functions aprint_normal(), aprint_naive(), aprint_verbose(), aprint_debug(), and aprint_error() are intended to be used to print autoconf(9) messages. their verbosity depends on flags set in the boothowto variable, through options passed during bootstrap; see boothowto(9) and βInteractive modeβ in boot(8):
AB_SILENTβ silent mode, enabled byboot -z.AB_QUIETβ quiet mode, enabled byboot -q.AB_VERBOSEβ verbose mode, enabled byboot -v.AB_DEBUGβ debug mode, enabled byboot -x.
the aprint_*() functions have the following behaviour, based on the above mentioned flags π
aprint_normal()β sends to the console unlessAB_QUIETis set. always sends to the log. dependable queen.aprint_naive()β sends to the console only ifAB_QUIETis set. never sends to the log. mysterious.aprint_verbose()β sends to the console only ifAB_VERBOSEis set. always sends to the log. yapper energy.aprint_debug()β sends to the console and the log only ifAB_DEBUGis set. exclusive club.aprint_error()β likeaprint_normal(), but it also keeps track of the number of times called. this allows a subsystem to report the number of errors that occurred during a quiet or silent initialization phase. she's keeping receipts, bestie π§Ύ
π¦ the _dev and _ifnet suffixes
for the aprint_*() functions there are two additional families of functions with the suffixes _dev and _ifnet which work like their counterparts without the suffixes, except that they take a device_t and struct ifnet *, respectively, as first argument, and prefix the log message with the corresponding device or interface name. so basically, they're the same but with a name tag attached π·οΈ
and aprint_get_error_count() reports the number of errors and resets the counter to 0. girlboss move: report + reset, in one call.
π the silent-mode plot twist
if AB_SILENT is set, none of the autoconfiguration message printing routines send output to the console. like, none of them. silence.
but β the AB_VERBOSE and AB_DEBUG flags override AB_SILENT. so if a chatty flag is on, the silence gets cancelled. π₯
π RETURN VALUES π
snprintf() and vsnprintf() return the number of characters that would have been placed in the buffer buf, if there was enough space in the buffer, not including the trailing NUL character used to terminate output strings like the user-space functions of the same name. yes, would have been. it counts the dream, not the reality π
tprintf_open() returns NULL if no terminal handle could be acquired. so like, check for NULL. please. π
π§ CODE REFERENCES π§
sys/kern/subr_prf.c
π SEE ALSO π
printf(1), printf(3), snprintb(3), boot(8), autoconf(9), boothowto(9) β go read them, they're all part of the same friend group π«Ά
π HISTORY π
in NetBSD 1.5 and earlier, printf() supported more format strings than the user space printf(). these nonstandard format strings are no longer supported. rip to a real one π
for the functionality provided by the former %b format string, see snprintb(3).
the aprint_normal(), aprint_naive(), aprint_verbose(), and aprint_debug() functions first appeared in BSD/OS. okay, legacy icons π
π BUGS π
uprintf() and ttyprintf() should be used sparingly, if at all. like, girl, be careful. π¬
where multiple lines of output are required to reach a process's controlling terminal, tprintf() is preferred. use the handle girlies, they're safer. no cap.
β so that's the whole tea β kernel formatted output conversion, fully documented, fully slay. now go forth and printf() responsibly. periodt. π
β¨
this page was brought to you by the Debian edition of the Kernel Developer's Manual, dated March 10, 2021 β mwah, xoxo π«Άπ
π¦ where this page comes from
- nixpkgs unstable: netbsd-source 11.0
- Copyright (c) 1998, 2002, 2007, 2011 The NetBSD Foundation, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms