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 5 . 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 5 . 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 Sy \! 192The message goes only to the system log. 193.It Sy ^ 194The message goes only to the console. 195.It Sy \? 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 1M . 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 1M 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 1M 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. However, if an unknown 300.Fa level 301is passed to 302.Fn cmn_err , 303the following panic error message is displayed: 304.Bd -literal 305panic: unknown level in cmn_err (level=level, msg=format) 306.Ed 307.Sh EXAMPLES 308.Bl -tag -width Ds 309.It Sy Example 1 No Using Fn cmn_err 310This first example shows how 311.Fn cmn_err 312can record tracing and debugging information only in the system log 313.Pq lines 17 ; 314display problems with a device only on the system console 315.Pq line 23 ; 316or display problems with the device on both the system console and in the system 317log 318.Pq line 28 . 319.Bd -literal 3201 struct reg { 3212 uchar_t data; 3223 uchar_t csr; 3234 }; 3245 3256 struct xxstate { 3267 ... 3278 dev_info_t *dip; 3289 struct reg *regp; 32910 ... 33011 }; 33112 33213 dev_t dev; 33314 struct xxstate *xsp; 33415 ... 33516 #ifdef DEBUG /* in debugging mode, log function call */ 33617 cmn_err(CE_CONT, "!%s%d: xxopen function called.", 33718 ddi_binding_name(xsp->dip), getminor(dev)); 33819 #endif /* end DEBUG */ 33920 ... 34021 /* display device power failure on system console */ 34122 if ((xsp->regp->csr & POWER) == OFF) 34223 cmn_err(CE_NOTE, "^OFF.", 34324 ddi_binding_name(xsp->dip), getminor(dev)); 34425 ... 34526 /* display warning if device has bad VTOC */ 34627 if (xsp->regp->csr & BADVTOC) 34728 cmn_err(CE_WARN, "%s%d: xxopen: Bad VTOC.", 34829 ddi_binding_name(xsp->dip), getminor(dev)); 349.Ed 350.It Sy Example 2 No Using the %b conversion specification 351This example shows how to use the 352.Sy %b 353conversion specification. 354Because of the leading 355.Qq Sy \? 356character in the format string, this message will always be logged, but it will 357only be displayed when the kernel is booted in verbose mode. 358.Bd -literal 359cmn_err(CE_CONT, "?reg=0x%b\en", regval, "\e020\e3Intr\e2Err\e1Enable"); 360.Ed 361.It Sy Example 3 No Using Fa regval 362When 363.Fa regval 364is set to 365.Pq decimal 36613, the following message would be displayed: 367.Bd -literal 368reg=0xd<Intr,,Enable> 369.Ed 370.It Sy Example 4 No Error Routine 371This example shows an error reporting routine which accepts a variable number 372of arguments and displays a single line error message both in the system log 373and on the system console. 374Note the use of 375.Fn vsprintf 376to construct the error message before calling 377.Fn cmn_err . 378.Bd -literal 379#include <sys/varargs.h> 380#include <sys/ddi.h> 381#include <sys/sunddi.h> 382#define MAX_MSG 256; 383 384void 385xxerror(dev_info_t *dip, int level, const char *fmt, ...) 386{ 387 va_list ap; 388 int instance; 389 char buf[MAX_MSG], *name; 390 391instance = ddi_get_instance(dip); 392name = ddi_binding_name(dip); 393 394/* format buf using fmt and arguments contained in ap */ 395 396va_start(ap, fmt); 397vsprintf(buf, fmt, ap); 398va_end(ap); 399 400/* pass formatted string to cmn_err(9F) */ 401 402cmn_err(level, "%s%d: %s", name, instance, buf); 403 404} 405.Ed 406.It Sy Example 5 No Log to Current Zone 407This example shows how messages can be sent to the log of the zone in which a 408thread is currently running, when applicable. 409Note that most hardware-related messages should instead be sent to the global 410zone using 411.Fn cmn_err . 412.Bd -literal 413zcmn_err(crgetzoneid(ddi_get_cred()), CE_NOTE, "out of processes"); 414.Ed 415.El 416.Sh SEE ALSO 417.Xr dmesg 1M , 418.Xr kernel 1M , 419.Xr zones 5 , 420.Xr ddi_binding_name 9F , 421.Xr ddi_cred 9F , 422.Xr ddi_driver_name 9F , 423.Xr ddi_get_instance 9F , 424.Xr sprintf 9F , 425.Xr va_arg 9F , 426.Xr va_end 9F , 427.Xr va_start 9F 428.Pp 429Writing Device Drivers 430.Sh WARNINGS 431The 432.Fn cmn_err 433function with the 434.Sy CE_CONT 435argument can be used by driver developers as a driver code debugging tool. 436However, using 437.Fn cmn_err 438in this capacity can change system timing characteristics. 439.Sh NOTES 440Messages of arbitrary length can be generated using 441.Fn cmn_err , 442but if the call to 443.Fn cmn_err 444is made from high-level interrupt context and insufficient memory is available 445to create a buffer of the specified size, the message will be truncated to 446LOG_MSGSIZE 447bytes 448.Pq see Pa sys/log.h . 449For this reason, callers of 450.Fn cmn_err 451that require complete and accurate message generation should post down from 452high-level interrupt context before calling 453.Fn cmn_err . 454