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 process p, and also to the logging facility β€” but only if pri is not -1.
  • log() β€” sends the message to the kernel logging facility, using the log level given by pri, 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, \10 gives octal and \20 gives 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