xref: /illumos-gate/usr/src/man/man9f/cmn_err.9f (revision 9b9d39d2a32ff806d2431dbcc50968ef1e6d46b2)
1.\"
2.\" The contents of this file are subject to the terms of the
3.\" Common Development and Distribution License (the "License").
4.\" You may not use this file except in compliance with the License.
5.\"
6.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
7.\" or http://www.opensolaris.org/os/licensing.
8.\" See the License for the specific language governing permissions
9.\" and limitations under the License.
10.\"
11.\" When distributing Covered Code, include this CDDL HEADER in each
12.\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
13.\" If applicable, add the following below this CDDL HEADER, with the
14.\" fields enclosed by brackets "[]" replaced with your own identifying
15.\" information: Portions Copyright [yyyy] [name of copyright owner]
16.\"
17.\"
18.\" Copyright 1989 AT&T
19.\" Copyright (c) 2006, Sun Microsystems, Inc. All Rights Reserved.
20.\" Copyright 2016 Nexenta Systems, Inc.
21.\"
22.Dd Dec 1, 2016
23.Dt CMN_ERR 9F
24.Os
25.Sh NAME
26.Nm cmn_err, dev_err, vcmn_err, zcmn_err, vzcmn_err
27.Nd display an error message or panic the system
28.Sh SYNOPSIS
29.In sys/cmn_err.h
30.In sys/ddi.h
31.In sys/sunddi.h
32.Ft void
33.Fo cmn_err
34.Fa "int level"
35.Fa "char *format"
36.Fa ...
37.Fc
38.Ft void
39.Fo dev_err
40.Fa "dev_info_t *dip"
41.Fa "int level"
42.Fa "char *format"
43.Fa ...
44.Fc
45.In sys/varargs.h
46.Ft void
47.Fo vcmn_err
48.Fa "int level"
49.Fa "char *format"
50.Fa "va_list ap"
51.Fc
52.In sys/types.h
53.Ft void
54.Fo zcmn_err
55.Fa "zoneid_t zoneid"
56.Fa "int level"
57.Fa "char *format"
58.Fa  ...
59.Fc
60.Ft void
61.Fo vzcmn_err
62.Fa "zoneid_t zoneid"
63.Fa "int level"
64.Fa "char *format"
65.Fa "va_list ap"
66.Fc
67.Sh INTERFACE LEVEL
68Architecture independent level 1 (DDI/DKI).
69.Sh PARAMETERS
70.Ss Fn cmn_err
71.Bl -tag -width Ds
72.It Fa level
73A constant indicating the severity of the error condition.
74.It Fa format
75Message to be displayed.
76.El
77.Ss Fn dev_err
78The
79.Fn dev_err
80function works exactly like
81.Fn cmn_err ,
82but includes an additional argument:
83.Bl -tag -width Ds
84.It Fa dip
85A pointer to the device's
86.Ft dev_info
87structure.
88.El
89.Ss Fn vcmn_err
90The
91.Fn vcmn_err
92function takes
93.Fa level
94and
95.Fa format
96as described for
97.Fn cmn_err ,
98but its third argument is different:
99.Bl -tag -width Ds
100.It Fa ap
101Variable argument list passed to the function.
102.El
103.Ss Fn zcmn_err
104The
105.Fn zcmn_err
106function works exactly like
107.Fn cmn_err ,
108but includes an additional argument:
109.Bl -tag -width Ds
110.It Fa zoneid
111Zone to which log messages should be directed.
112See
113.Xr zones 7 .
114.El
115.Ss Fn vzcmn_err
116The
117.Fn vzcmn_err
118function works exactly like
119.Fn vcmn_err ,
120but includes an additional argument:
121.Bl -tag -width Ds
122.It Fa zoneid
123Zone to which log messages should be directed.
124See
125.Xr zones 7 .
126.El
127.Sh DESCRIPTION
128.Ss Fn cmn_err
129The
130.Fn cmn_err
131function displays a specified message on the console.
132.Fn cmn_err
133can also panic the system.
134When the system panics, it attempts to save recent changes to data, display a
135.Qq panic message
136on the console, attempt to write a core file, and halt system processing.
137See the
138.Sy CE_PANIC
139.Fa level
140below.
141.Pp
142.Fa level
143is a constant indicating the severity of the error condition.
144The four severity levels are:
145.Bl -tag -width "CE_PANIC"
146.It Sy CE_CONT
147Used to continue another message or to display an informative message not
148associated with an error.
149Note that multiple
150.Sy CE_CONT
151messages without a newline may or may not appear on the system console or in the
152system log as a single line message.
153A single line message may be produced by constructing the message with
154.Xr sprintf 9F
155or
156.Xr vsprintf 9F
157before calling
158.Fn cmn_err .
159.It Sy CE_NOTE
160Used to display a message preceded with
161.Sy NOTICE .
162This message is used to report system events that do not necessarily require
163user action, but may interest the system administrator.
164For example, a message saying that a sector on a disk needs to be accessed
165repeatedly before it can be accessed correctly might be noteworthy.
166.It Sy CE_WARN
167Used to display a message preceded with
168.Sy WARNING .
169This message is used to report system events that require immediate attention,
170such as those where if an action is not taken, the system may panic.
171For example, when a peripheral device does not initialize correctly, this level
172should be used.
173.It Sy CE_PANIC
174Used to display a message preceded with
175.Qq Sy panic ,
176and to panic the system.
177Drivers should specify this level only under the most severe conditions or when
178debugging a driver.
179A valid use of this level is when the system cannot continue to function.
180If the error is recoverable, or not essential to continued system operation, do
181not panic the system.
182.El
183.Pp
184The
185.Fa format
186is identical to the one described in
187.Xr sprintf 9F
188with additional meaning of the first character affecting where the message will
189be written:
190.Bl -tag -width Ds
191.It \&!
192The message goes only to the system log.
193.It Sy ^
194The message goes only to the console.
195.It \&?
196If
197.Fa level
198is also
199.Sy CE_CONT ,
200the message is always sent to the system log, but is only written to the console
201when the system has been booted in verbose mode.
202See
203.Xr kernel 8 .
204If neither condition is met, the
205.Qq Sy \&?
206character has no effect and is simply ignored.
207.El
208.Pp
209Refer to
210.Xr syslogd 8
211to determine where the system log is written.
212.Pp
213The
214.Fn cmn_err
215function sends log messages to the log of the global zone.
216.Fn cmn_err
217appends a
218.Sy \en
219to each
220.Fa format ,
221except when
222.Fa level
223is
224.Sy CE_CONT .
225.Ss Fn dev_err
226With the exception of its first argument
227.Pq Fa dip ,
228.Fn dev_err
229is identical to
230.Fn cmn_err .
231.Fa dip
232is a pointer to a device's
233.Ft dev_info
234structure, which is used to prepend the driver name and instance number to the
235message.
236The driver name and instance number are retrieved using
237.Xr ddi_driver_name 9F
238and
239.Xr ddi_get_instance 9F .
240.Ss Fn vcmn_err
241The
242.Fn vcmn_err
243function is identical to
244.Fn cmn_err
245except that its last argument,
246.Fa ap ,
247is a pointer to a variable list of arguments.
248.Fa ap
249contains the list of arguments used by the conversion specifications in
250.Fa format .
251.Fa ap
252must be initialized by calling
253.Xr va_start 9F .
254.Xr va_end 9F
255is used to clean up and must be called after each traversal of the list.
256Multiple traversals of the argument list, each bracketed by
257.Xr va_start 9F
258and
259.Xr va_end 9F ,
260are possible.
261.Ss Fn zcmn_err
262With the exception of its first argument
263.Pq Fa zoneid ,
264.Fn zcmn_err
265is identical to
266.Fn cmn_err .
267.Fa zoneid
268is the numeric ID of the zone to which the message should be directed.
269Note that
270.Fa zoneid
271only has an effect if the message is sent to the system log.
272Using
273.Fa zoneid
274will cause messages to be sent to the log associated with the specified local
275zone rather than the log in the global zone.
276This is accomplished by the message being received and processed by the
277.Xr syslogd 8
278process running in the specified zone instead of the one running in the global
279zone.
280You can retrieve a process zone ID from its credential structure using
281.Xr crgetzoneid 9F .
282.Ss Fn vzcmn_err
283With the exception of its first argument
284.Pq Fa zoneid ,
285.Fn vzcmn_err
286is identical to
287.Fn vcmn_err .
288See the description of
289.Fn zcmn_err
290above for an explanation on how the
291.Fa zoneid
292argument is handled.
293.Sh CONTEXT
294The
295.Fn cmn_err
296function can be called from user, kernel, interrupt, or high-level interrupt
297context.
298.Sh RETURN VALUES
299None.
300However, if an unknown
301.Fa level
302is passed to
303.Fn cmn_err ,
304the following panic error message is displayed:
305.Bd -literal
306panic: unknown level in cmn_err (level=level, msg=format)
307.Ed
308.Sh EXAMPLES
309.Bl -tag -width Ds
310.It Sy Example 1 No Using Fn cmn_err
311This first example shows how
312.Fn cmn_err
313can record tracing and debugging information only in the system log
314.Pq lines 17 ;
315display problems with a device only on the system console
316.Pq line 23 ;
317or display problems with the device on both the system console and in the system
318log
319.Pq line 28 .
320.Bd -literal
3211  struct  reg {
3222          uchar_t data;
3233          uchar_t csr;
3244  };
3255
3266  struct  xxstate {
3277          ...
3288          dev_info_t *dip;
3299          struct reg *regp;
33010         ...
33111  };
33212
33313  dev_t dev;
33414  struct xxstate *xsp;
33515    ...
33616  #ifdef DEBUG    /* in debugging mode, log function call */
33717     cmn_err(CE_CONT, "!%s%d: xxopen function called.",
33818          ddi_binding_name(xsp->dip), getminor(dev));
33919  #endif  /* end DEBUG */
34020    ...
34121  /* display device power failure on system console */
34222     if ((xsp->regp->csr & POWER) == OFF)
34323          cmn_err(CE_NOTE, "^OFF.",
34424               ddi_binding_name(xsp->dip), getminor(dev));
34525    ...
34626  /* display warning if device has bad VTOC */
34727     if (xsp->regp->csr & BADVTOC)
34828          cmn_err(CE_WARN, "%s%d: xxopen: Bad VTOC.",
34929               ddi_binding_name(xsp->dip), getminor(dev));
350.Ed
351.It Sy Example 2 No Using the %b conversion specification
352This example shows how to use the
353.Sy %b
354conversion specification.
355Because of the leading
356.Qq Sy \&?
357character in the format string, this message will always be logged, but it will
358only be displayed when the kernel is booted in verbose mode.
359.Bd -literal
360cmn_err(CE_CONT, "?reg=0x%b\en", regval, "\e020\e3Intr\e2Err\e1Enable");
361.Ed
362.It Sy Example 3 No Using Fa regval
363When
364.Fa regval
365is set to
366.Pq decimal
36713, the following message would be displayed:
368.Bd -literal
369reg=0xd<Intr,,Enable>
370.Ed
371.It Sy Example 4 No Error Routine
372This example shows an error reporting routine which accepts a variable number
373of arguments and displays a single line error message both in the system log
374and on the system console.
375Note the use of
376.Fn vsprintf
377to construct the error message before calling
378.Fn cmn_err .
379.Bd -literal
380#include <sys/varargs.h>
381#include <sys/ddi.h>
382#include <sys/sunddi.h>
383#define MAX_MSG 256;
384
385void
386xxerror(dev_info_t *dip, int level, const char *fmt, ...)
387{
388    va_list     ap;
389    int         instance;
390    char        buf[MAX_MSG], *name;
391
392instance = ddi_get_instance(dip);
393name = ddi_binding_name(dip);
394
395/* format buf using fmt and arguments contained in ap */
396
397va_start(ap, fmt);
398vsprintf(buf, fmt, ap);
399va_end(ap);
400
401/* pass formatted string to cmn_err(9F) */
402
403cmn_err(level, "%s%d: %s", name, instance, buf);
404
405}
406.Ed
407.It Sy Example 5 No Log to Current Zone
408This example shows how messages can be sent to  the log of the zone in which a
409thread is currently running, when applicable.
410Note that most hardware-related messages should instead be sent to the global
411zone using
412.Fn cmn_err .
413.Bd -literal
414zcmn_err(crgetzoneid(ddi_get_cred()), CE_NOTE, "out of processes");
415.Ed
416.El
417.Sh SEE ALSO
418.Xr zones 7 ,
419.Xr dmesg 8 ,
420.Xr kernel 8 ,
421.Xr ddi_binding_name 9F ,
422.Xr ddi_cred 9F ,
423.Xr ddi_driver_name 9F ,
424.Xr ddi_get_instance 9F ,
425.Xr sprintf 9F ,
426.Xr va_arg 9F ,
427.Xr va_end 9F ,
428.Xr va_start 9F
429.Pp
430Writing Device Drivers
431.Sh WARNINGS
432The
433.Fn cmn_err
434function with the
435.Sy CE_CONT
436argument can be used by driver developers as a driver code debugging tool.
437However, using
438.Fn cmn_err
439in this capacity can change system timing characteristics.
440.Sh NOTES
441Messages of arbitrary length can be generated using
442.Fn cmn_err ,
443but if the call to
444.Fn cmn_err
445is made from high-level interrupt context and insufficient memory is available
446to create a buffer of the specified size, the message will be truncated to
447LOG_MSGSIZE
448bytes
449.Pq see Pa sys/log.h .
450For this reason, callers of
451.Fn cmn_err
452that require complete and accurate message generation should post down from
453high-level interrupt context before calling
454.Fn cmn_err .
455