.\" .\" Copyright (c) 2001 Andrew R. Reiter .\" Copyright (c) 2004 Joerg Wunsch .\" All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES .\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. .\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, .\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, .\" BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; .\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED .\" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, .\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" .\" $FreeBSD: src/share/man/man9/printf.9,v 1.8 2006/09/08 14:05:03 ru Exp $ .\" .Dd March 13, 2020 .Dt KPRINTF 9 .Os .Sh NAME .Nm kprintf , .Nm ksprintf , .Nm ksnprintf , .Nm kvprintf , .Nm kvsprintf , .Nm kvsnprintf , .Nm krateprintf , .Nm tprintf , .Nm uprintf , .Nm log .Nd formatted output conversion .Sh SYNOPSIS .In sys/types.h .In sys/systm.h .Ft int .Fn kprintf "const char *format" ... .Ft int .Fn ksprintf "char *str" "const char *format" ... .Ft int .Fn ksnprintf "char *str" "size_t size" "const char *format" ... .Ft int .Fn kvprintf "const char *format" "__va_list ap" .Ft int .Fn kvsprintf "char *str" "const char *format" "__va_list ap" .Ft int .Fn kvsnprintf "char *str" "size_t size" "const char *format" "__va_list ap" .Ft int .Fn krateprintf "struct krate *rate" "const char *format" ... .Ft int .Fn uprintf "const char *format" ... .In sys/tprintf.h .Ft int .Fn tprintf "struct proc *p" "int pri" "const char *format" ... .In sys/syslog.h .Ft int .Fn log "int pri" "const char *format" ... .Sh DESCRIPTION The .Nm family of functions are similar to the .Xr printf 3 family of functions. The different functions each use a different output stream. The .Fn uprintf function outputs to the current process' controlling tty, while .Fn kprintf , .Fn ksprintf , .Fn ksnprintf , .Fn kvprintf , .Fn kvsprintf and .Fn kvsnprintf write to the console as well as to the logging facility. The .Fn tprintf function outputs to the tty associated with the process .Fa p and the logging facility if .Fa pri is not \-1. The .Fn log function sends the message to the kernel logging facility, using the log level as indicated by .Fa pri . .Pp Each of these related functions use the .Fa format , .Fa str , .Fa size and .Fa ap parameters in the same manner as .Xr printf 3 . However, the .Nm functions add another conversion specifier to .Fa format : .Pp The .Cm \&%pb%i identifier expects two arguments: an .Vt "char *" and a .Vt int . These are used as a register value and a print mask for decoding bitmasks. The print mask is made up of two parts: the base and the arguments. The base value is the output base expressed as an integer value; for example, \e10 gives octal and \e20 gives hexadecimal. The arguments are made up of a sequence of bit identifiers. Each bit identifier begins with an integer value which is the number of the bit (starting from 1) this identifier describes. The rest of the identifier is a string of characters containing the name of the bit. The string is terminated by either the bit number at the start of the next bit identifier or .Dv NUL for the last bit identifier. .Pp The .Fn log function uses .Xr syslog 3 level values .Dv LOG_DEBUG through .Dv LOG_EMERG for its .Fa pri parameter (mistakenly called .Sq priority here). Alternatively, if a .Fa pri of \-1 is given, the message will be appended to the last log message started by a previous call to .Fn log . As these messages are generated by the kernel itself, the facility will always be .Dv LOG_KERN . .Pp The .Fn krateprintf function is a rate controlled version of .Fn kprintf . The .Fa freq member of the .Vt struct krate pointed to by .Fa rate must be initialized with the desired reporting frequency. A .Fa freq of 0 will result in no output. Initializing .Fa count to a negative value allows an initial burst. .Sh RETURN VALUES The .Fn kprintf , .Fn ksprintf , .Fn ksnprintf , .Fn kvprintf , .Fn kvsprintf , .Fn kvsnprintf , .Fn tprintf , .Fn uprintf , and .Fn log functions return the number of characters displayed. .Pp The .Fn krateprintf function returns 1 or 0, depending on whether anything was printed or not, allowing code to issue additional .Fn kprintf Ns s if desired. .Sh LOADER TUNABLES Tunables can be set at the .Xr loader 8 prompt before booting the kernel or stored in .Xr loader.conf 5 . .Bl -tag -width "xxxxxx" .It Va kern.kprintf_logging A bit mask that specifies the targets of .Fn kprintf . Supported targets are the console (0x1) and the dmesg buffer (0x4). The default value is 5 (print to both). .It Va kern.log_console_output Specifies whether console output is duplicated to the syslog. The default value is 1. .It Va security.ptr_restrict Specifying 1 masks out the upper bits of pointers printed with %p, and specifying 2 masks out pointer values altogether. The default value is 0. .It Va security.unprivileged_read_msgbuf Specifies if unprivileged processes may read the kernel buffer. The default value is 1. .El .Sh SYSCTL VARIABLES The loader tunables described in .Sx LOADER TUNABLES are also available as sysctl(8) variables of the same names. .Sh EXAMPLES This example demonstrates the use of the .Cm \&%pb%i conversion specifier. The function .Bd -literal -offset indent void kprintf_test(void) { kprintf("reg=%pb%i\en", "\e10\e2BITTWO\e1BITONE\en", 3); } .Ed .Pp will produce the following output: .Bd -literal -offset indent reg=3 .Ed .Pp The call .Bd -literal -offset indent log(LOG_DEBUG, "%s%d: been there.\en", sc->sc_name, sc->sc_unit); .Ed .Pp will add the appropriate debug message at priority .Dq Li kern.debug to the system log. .Sh SEE ALSO .Xr printf 3 , .Xr syslog 3