1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
.\" $NetBSD: kprintf.9,v 1.41 2025/01/12 20:16:34 rillig Exp $
.\"
.\" Copyright (c) 1998, 2002, 2007, 2011 The NetBSD Foundation, Inc.
.\" All rights reserved.
.\"
.\" This code is derived from software contributed to The NetBSD Foundation
.\" by Jeremy Cooper and by Jason R. Thorpe.
.\"
.\" 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
.\" ``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 FOUNDATION OR CONTRIBUTORS
.\" 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.
.\"
.Dd March 10, 2021
.Dt KPRINTF 9
.Os
.Sh NAME
.Nm device_printf ,
.Nm printf ,
.Nm printf_nolog ,
.Nm printf_nostamp ,
.Nm snprintf ,
.Nm vasprintf ,
.Nm vprintf ,
.Nm vsnprintf ,
.Nm uprintf ,
.Nm ttyprintf ,
.Nm tprintf_open ,
.Nm tprintf ,
.Nm tprintf_close ,
.Nm aprint_normal ,
.Nm aprint_naive ,
.Nm aprint_verbose ,
.Nm aprint_debug ,
.Nm aprint_error ,
.Nm aprint_normal_dev ,
.Nm aprint_naive_dev ,
.Nm aprint_verbose_dev ,
.Nm aprint_debug_dev ,
.Nm aprint_error_dev ,
.Nm aprint_normal_ifnet ,
.Nm aprint_naive_ifnet ,
.Nm aprint_verbose_ifnet ,
.Nm aprint_debug_ifnet ,
.Nm aprint_error_ifnet ,
.Nm aprint_get_error_count
.Nd kernel formatted output conversion
.Sh SYNOPSIS
.In sys/systm.h
.Ft void
.Fn device_printf "device_t" "const char *format" "..."
.Ft void
.Fn printf "const char *format" "..."
.Ft void
.Fn printf_nolog "const char *format" "..."
.Ft void
.Fn printf_nostamp "const char *format" "..."
.Ft int
.Fn snprintf "char *buf" "size_t size" "const char *format" "..."
.Ft int
.Fn vasprintf "char **buf" "const char *format" "va_list ap"
.Ft void
.Fn vprintf "const char *format" "va_list ap"
.Ft int
.Fn vsnprintf "char *buf" "size_t size" "const char *format" "va_list ap"
.Ft void
.Fn uprintf "const char *format" "..."
.Ft void
.Fn ttyprintf "struct tty *tty" "const char *format" "..."
.In sys/tprintf.h
.Ft tpr_t
.Fn tprintf_open "struct proc *p"
.Ft void
.Fn tprintf "tpr_t tpr" "const char *format" "..."
.Ft void
.Fn tprintf_close "tpr_t tpr"
.Ft void
.Fn aprint_normal "const char *format" "..."
.Ft void
.Fn aprint_naive "const char *format" "..."
.Ft void
.Fn aprint_verbose "const char *format" "..."
.Ft void
.Fn aprint_debug "const char *format" "..."
.Ft void
.Fn aprint_error "const char *format" "..."
.Ft void
.Fn aprint_normal_dev "device_t" "const char *format" "..."
.Ft void
.Fn aprint_naive_dev "device_t" "const char *format" "..."
.Ft void
.Fn aprint_verbose_dev "device_t" "const char *format" "..."
.Ft void
.Fn aprint_debug_dev "device_t" "const char *format" "..."
.Ft void
.Fn aprint_error_dev "device_t" "const char *format" "..."
.Ft void
.Fn aprint_normal_ifnet "struct ifnet *" "const char *format" "..."
.Ft void
.Fn aprint_naive_ifnet "struct ifnet *" "const char *format" "..."
.Ft void
.Fn aprint_verbose_ifnet "struct ifnet *" "const char *format" "..."
.Ft void
.Fn aprint_debug_ifnet "struct ifnet *" "const char *format" "..."
.Ft void
.Fn aprint_error_ifnet "struct ifnet *" "const char *format" "..."
.Ft int
.Fn aprint_get_error_count "void"
.Sh DESCRIPTION
The
.Fn printf
family of functions allows the kernel to send formatted messages to various
output devices.
The functions
.Fn printf
and
.Fn vprintf
send formatted strings to the system console.
The
.Fn device_printf
function is identical to
.Fn printf ,
except that it prefixes the log message with the corresponding
device name.
The
.Fn printf_nolog
function is identical to
.Fn printf ,
except it does not send the data to the system log.
The
.Fn printf_nostamp
function is identical to
.Fn printf ,
except it does not prefix the output with a timestamp.
The functions
.Fn snprintf ,
.Fn vasprintf ,
and
.Fn vsnprintf
write output to a string buffer.
These five functions work similarly to their user space counterparts,
and are not described in detail here.
The
.Fn vasprintf
function allocates memory with
.Xr kmem_alloc 9
and it is the caller's responsibility to free the returned string with
.Xr kmem_free 9 .
.Pp
The functions
.Fn uprintf
and
.Fn ttyprintf
send formatted strings to the current process's controlling tty and a specific
tty, respectively.
.Pp
The
.Fn tprintf
function sends formatted strings to a process's controlling tty,
via a handle of type tpr_t.
This allows multiple write operations to the tty with a guarantee that the
tty will be valid across calls.
A handle is acquired by calling
.Fn tprintf_open
with the target process as an argument.
This handle must be closed with a matching call to
.Fn tprintf_close .
.Pp
The functions
.Fn aprint_normal ,
.Fn aprint_naive ,
.Fn aprint_verbose ,
.Fn aprint_debug ,
and
.Fn aprint_error
are intended to be used to print
.Xr autoconf 9
messages.
Their verbosity depends on flags set in the
.Va boothowto
variable, through options passed during bootstrap; see
.Xr boothowto 9
and
.Sx Interactive mode
in
.Xr boot 8 :
.Bl -tag -width AB_VERBOSE
.It Dv AB_SILENT
silent mode, enabled by
.Ic boot
.Fl z .
.It Dv AB_QUIET
quiet mode, enabled by
.Ic boot
.Fl q .
.It Dv AB_VERBOSE
verbose mode, enabled by
.Ic boot
.Fl v .
.It Dv AB_DEBUG
debug mode, enabled by
.Ic boot
.Fl x .
.El
.Pp
The
.Fn aprint_*
functions have the following behaviour, based on the above
mentioned flags:
.Bl -tag -width Xaprint_verboseXXX
.It Fn aprint_normal
Sends to the console unless
.Dv AB_QUIET
is set.
Always sends to the log.
.It Fn aprint_naive
Sends to the console only if
.Dv AB_QUIET
is set.
Never sends to the log.
.It Fn aprint_verbose
Sends to the console only if
.Dv AB_VERBOSE
is set.
Always sends to the log.
.It Fn aprint_debug
Sends to the console and the log only if
.Dv AB_DEBUG
is set.
.It Fn aprint_error
Like
.Fn aprint_normal ,
but 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.
.El
.Pp
For the
.Fn aprint_*
functions there are two additional families of functions with the
suffixes
.Dv _dev
and
.Dv _ifnet
which work like their counterparts without the suffixes, except that
they take a
.Ft device_t
and
.Ft struct ifnet * ,
respectively, as first argument,
and prefix the log message with the
corresponding device or interface name.
.Pp
The
.Fn aprint_get_error_count
function reports the number of errors and resets the counter to 0.
.Pp
If
.Dv AB_SILENT
is set, none of the autoconfiguration message printing routines send output
to the console.
The
.Dv AB_VERBOSE
and
.Dv AB_DEBUG
flags override
.Dv AB_SILENT .
.Sh RETURN VALUES
The
.Fn snprintf
and
.Fn vsnprintf
functions return the number of characters that would have been placed
in the buffer
.Fa buf ,
if there was enough space in the buffer, not including the trailing
.Dv NUL
character used to terminate output strings like the user-space functions
of the same name.
.Pp
The
.Fn tprintf_open
function returns
.Dv NULL
if no terminal handle could be acquired.
.Sh CODE REFERENCES
.Pa sys/kern/subr_prf.c
.Sh SEE ALSO
.Xr printf 1 ,
.Xr printf 3 ,
.Xr snprintb 3 ,
.Xr boot 8 ,
.Xr autoconf 9 ,
.Xr boothowto 9
.Sh HISTORY
In
.Nx 1.5
and earlier,
.Fn printf
supported more format strings than the user space
.Fn printf .
These nonstandard format strings are no longer supported.
For the functionality provided by the former
.Li %b
format string, see
.Xr snprintb 3 .
.Pp
The
.Fn aprint_normal ,
.Fn aprint_naive ,
.Fn aprint_verbose ,
and
.Fn aprint_debug
functions first appeared in
.Bsx .
.Sh BUGS
The
.Fn uprintf
and
.Fn ttyprintf
functions should be used sparingly, if at all.
Where multiple lines of output are required to reach a process's
controlling terminal,
.Fn tprintf
is preferred.
|