xref: /dragonfly/share/man/man9/kprintf.9 (revision 1a05b9d1)
1.\"
2.\" Copyright (c) 2001 Andrew R. Reiter
3.\" Copyright (c) 2004 Joerg Wunsch
4.\" All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\"
15.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20.\" BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22.\" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25.\" SUCH DAMAGE.
26.\"
27.\" $FreeBSD: src/share/man/man9/printf.9,v 1.8 2006/09/08 14:05:03 ru Exp $
28.\"
29.Dd March 13, 2020
30.Dt KPRINTF 9
31.Os
32.Sh NAME
33.Nm kprintf ,
34.Nm ksprintf ,
35.Nm ksnprintf ,
36.Nm kvprintf ,
37.Nm kvsprintf ,
38.Nm kvsnprintf ,
39.Nm krateprintf ,
40.Nm tprintf ,
41.Nm uprintf ,
42.Nm log
43.Nd formatted output conversion
44.Sh SYNOPSIS
45.In sys/types.h
46.In sys/systm.h
47.Ft int
48.Fn kprintf "const char *format" ...
49.Ft int
50.Fn ksprintf "char *str" "const char *format" ...
51.Ft int
52.Fn ksnprintf "char *str" "size_t size" "const char *format" ...
53.Ft int
54.Fn kvprintf "const char *format" "__va_list ap"
55.Ft int
56.Fn kvsprintf "char *str" "const char *format" "__va_list ap"
57.Ft int
58.Fn kvsnprintf "char *str" "size_t size" "const char *format" "__va_list ap"
59.Ft int
60.Fn krateprintf "struct krate *rate" "const char *format" ...
61.Ft int
62.Fn uprintf "const char *format" ...
63.In sys/tprintf.h
64.Ft int
65.Fn tprintf "struct proc *p" "int pri" "const char *format" ...
66.In sys/syslog.h
67.Ft int
68.Fn log "int pri" "const char *format" ...
69.Sh DESCRIPTION
70The
71.Nm
72family of functions are similar to the
73.Xr printf 3
74family of functions.
75The different functions each use a different output stream.
76The
77.Fn uprintf
78function outputs to the current process' controlling tty, while
79.Fn kprintf ,
80.Fn ksprintf ,
81.Fn ksnprintf ,
82.Fn kvprintf ,
83.Fn kvsprintf
84and
85.Fn kvsnprintf
86write to the console as well as to the logging facility.
87The
88.Fn tprintf
89function outputs to the tty associated with the process
90.Fa p
91and the logging facility if
92.Fa pri
93is not \-1.
94The
95.Fn log
96function sends the message to the kernel logging facility, using
97the log level as indicated by
98.Fa pri .
99.Pp
100Each of these related functions use the
101.Fa format ,
102.Fa str ,
103.Fa size
104and
105.Fa va
106parameters in the same manner as
107.Xr printf 3 .
108However, the
109.Nm
110functions add another conversion specifier to
111.Fa format :
112.Pp
113The
114.Cm \&%pb%i
115identifier expects two arguments: an
116.Vt "char *"
117and a
118.Vt int .
119These are used as a register value and a print mask for decoding bitmasks.
120The print mask is made up of two parts: the base and the
121arguments.
122The base value is the output base expressed as an integer value;
123for example, \e10 gives octal and \e20 gives hexadecimal.
124The arguments are made up of a sequence of bit identifiers.
125Each bit identifier begins with an integer value which is the number of the
126bit (starting from 1) this identifier describes.
127The rest of the identifier is a string of characters containing the name of
128the bit.
129The string is terminated by either the bit number at the start of the next
130bit identifier or
131.Dv NUL
132for the last bit identifier.
133.Pp
134The
135.Fn log
136function uses
137.Xr syslog 3
138level values
139.Dv LOG_DEBUG
140through
141.Dv LOG_EMERG
142for its
143.Fa pri
144parameter (mistakenly called
145.Sq priority
146here).
147Alternatively, if a
148.Fa pri
149of \-1 is given, the message will be appended to the last log message
150started by a previous call to
151.Fn log .
152As these messages are generated by the kernel itself, the facility will
153always be
154.Dv LOG_KERN .
155.Pp
156The
157.Fn krateprintf
158function is a rate controlled version of
159.Fn kprintf .
160The
161.Fa freq
162member of the
163.Vt struct krate
164pointed to by
165.Fa rate
166must be initialized with the desired reporting frequency.
167A
168.Fa freq
169of 0 will result in no output.
170Initializing
171.Fa count
172to a negative value allows an initial burst.
173.Sh RETURN VALUES
174The
175.Fn kprintf ,
176.Fn ksprintf ,
177.Fn ksnprintf ,
178.Fn kvprintf ,
179.Fn kvsprintf ,
180.Fn kvsnprintf ,
181.Fn tprintf ,
182.Fn uprintf ,
183and
184.Fn log
185functions return the number of characters displayed.
186.Pp
187The
188.Fn krateprintf
189function returns 1 or 0, depending on whether anything was printed or not,
190allowing code to issue additional
191.Fn kprintf Ns s
192if desired.
193.Sh LOADER TUNABLES
194Tunables can be set at the
195.Xr loader 8
196prompt before booting the kernel or stored in
197.Xr loader.conf 5 .
198.Bl -tag -width "xxxxxx"
199.It Va kern.kprintf_logging
200A bit mask that specifies the targets of
201.Fn kprintf .
202Supported targets are the console (0x1) and the dmesg buffer (0x4).
203The default value is 5 (print to both).
204.It Va kern.log_console_output
205Specifies whether console output is duplicated to the syslog.
206The default value is 1.
207.It Va security.ptr_restrict
208Specifying 1 masks out the upper bits of pointers printed  with %p, and
209specifying 2 masks out pointer values altogether.
210The default value is 0.
211.It Va security.unprivileged_read_msgbuf
212Specifies if unprivileged processes may read the kernel buffer.
213The default value is 1.
214.El
215.Sh SYSCTL VARIABLES
216The loader tunables described in
217.Sx LOADER TUNABLES
218are also available as sysctl(8) variables of the same names.
219.Sh EXAMPLES
220This example demonstrates the use of the
221.Cm \&%pb%i
222conversion specifier.
223The function
224.Bd -literal -offset indent
225void
226kprintf_test(void)
227{
228	kprintf("reg=%pb%i\en", "\e10\e2BITTWO\e1BITONE\en", 3);
229}
230.Ed
231.Pp
232will produce the following output:
233.Bd -literal -offset indent
234reg=3<BITTWO,BITONE>
235.Ed
236.Pp
237The call
238.Bd -literal -offset indent
239log(LOG_DEBUG, "%s%d: been there.\en", sc->sc_name, sc->sc_unit);
240.Ed
241.Pp
242will add the appropriate debug message at priority
243.Dq Li kern.debug
244to the system log.
245.Sh SEE ALSO
246.Xr printf 3 ,
247.Xr syslog 3
248