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.\" Copyright 2024 Oxide Computer Company 22.\" 23.Dd September 15, 2024 24.Dt CMN_ERR 9F 25.Os 26.Sh NAME 27.Nm cmn_err, dev_err, vcmn_err, zcmn_err, vzcmn_err 28.Nd display an error message or panic the system 29.Sh SYNOPSIS 30.In sys/cmn_err.h 31.In sys/ddi.h 32.In sys/sunddi.h 33.Ft void 34.Fo cmn_err 35.Fa "int level" 36.Fa "char *format" 37.Fa ... 38.Fc 39.Ft void 40.Fo dev_err 41.Fa "dev_info_t *dip" 42.Fa "int level" 43.Fa "char *format" 44.Fa ... 45.Fc 46.In sys/varargs.h 47.Ft void 48.Fo vcmn_err 49.Fa "int level" 50.Fa "char *format" 51.Fa "va_list ap" 52.Fc 53.Ft void 54.Fo vdev_err 55.Fa "dev_info_t *dip" 56.Fa "int level" 57.Fa "char *format" 58.Fa "va_list ap" 59.Fc 60.In sys/types.h 61.Ft void 62.Fo zcmn_err 63.Fa "zoneid_t zoneid" 64.Fa "int level" 65.Fa "char *format" 66.Fa ... 67.Fc 68.Ft void 69.Fo vzcmn_err 70.Fa "zoneid_t zoneid" 71.Fa "int level" 72.Fa "char *format" 73.Fa "va_list ap" 74.Fc 75.Sh INTERFACE LEVEL 76Architecture independent level 1 (DDI/DKI). 77.Sh PARAMETERS 78.Ss Fn cmn_err 79.Bl -tag -width Ds 80.It Fa level 81A constant indicating the severity of the error condition. 82.It Fa format 83Message to be displayed. 84.El 85.Ss Fn dev_err 86The 87.Fn dev_err 88function works exactly like 89.Fn cmn_err , 90but includes an additional argument: 91.Bl -tag -width Ds 92.It Fa dip 93A pointer to the device's 94.Ft dev_info 95structure. 96.El 97.Ss Fn vcmn_err 98The 99.Fn vcmn_err 100function takes 101.Fa level 102and 103.Fa format 104as described for 105.Fn cmn_err , 106but its third argument is different: 107.Bl -tag -width Ds 108.It Fa ap 109Variable argument list passed to the function. 110.El 111.Ss Fn vdev_err 112The 113.Fn vdev_err 114function takes 115.Fa dip , 116.Fa level , 117and 118.Fa format 119as described for 120.Fn dev_err , 121but its fourth argument is different: 122.Bl -tag -width Ds 123.It Fa ap 124Variable argument list passed to the function. 125.El 126.Ss Fn zcmn_err 127The 128.Fn zcmn_err 129function works exactly like 130.Fn cmn_err , 131but includes an additional argument: 132.Bl -tag -width Ds 133.It Fa zoneid 134Zone to which log messages should be directed. 135See 136.Xr zones 7 . 137.El 138.Ss Fn vzcmn_err 139The 140.Fn vzcmn_err 141function works exactly like 142.Fn vcmn_err , 143but includes an additional argument: 144.Bl -tag -width Ds 145.It Fa zoneid 146Zone to which log messages should be directed. 147See 148.Xr zones 7 . 149.El 150.Sh DESCRIPTION 151.Ss Fn cmn_err 152The 153.Fn cmn_err 154function displays a specified message on the console. 155.Fn cmn_err 156can also panic the system. 157When the system panics, it attempts to save recent changes to data, display a 158.Qq panic message 159on the console, attempt to write a core file, and halt system processing. 160See the 161.Sy CE_PANIC 162.Fa level 163below. 164.Pp 165.Fa level 166is a constant indicating the severity of the error condition. 167The four severity levels are: 168.Bl -tag -width "CE_PANIC" 169.It Sy CE_CONT 170Used to continue another message or to display an informative message not 171associated with an error. 172Note that multiple 173.Sy CE_CONT 174messages without a newline may or may not appear on the system console or in the 175system log as a single line message. 176A single line message may be produced by constructing the message with 177.Xr sprintf 9F 178or 179.Xr vsprintf 9F 180before calling 181.Fn cmn_err . 182.It Sy CE_NOTE 183Used to display a message preceded with 184.Sy NOTICE . 185This message is used to report system events that do not necessarily require 186user action, but may interest the system administrator. 187For example, a message saying that a sector on a disk needs to be accessed 188repeatedly before it can be accessed correctly might be noteworthy. 189.It Sy CE_WARN 190Used to display a message preceded with 191.Sy WARNING . 192This message is used to report system events that require immediate attention, 193such as those where if an action is not taken, the system may panic. 194For example, when a peripheral device does not initialize correctly, this level 195should be used. 196.It Sy CE_PANIC 197Used to display a message preceded with 198.Qq Sy panic , 199and to panic the system. 200Drivers should specify this level only under the most severe conditions or when 201debugging a driver. 202A valid use of this level is when the system cannot continue to function. 203If the error is recoverable, or not essential to continued system operation, do 204not panic the system. 205.El 206.Pp 207The 208.Fa format 209is identical to the one described in 210.Xr sprintf 9F 211with additional meaning of the first character affecting where the message will 212be written: 213.Bl -tag -width Ds 214.It \&! 215The message goes only to the system log. 216.It Sy ^ 217The message goes only to the console. 218.It \&? 219If 220.Fa level 221is also 222.Sy CE_CONT , 223the message is always sent to the system log, but is only written to the console 224when the system has been booted in verbose mode. 225See 226.Xr kernel 8 . 227If neither condition is met, the 228.Qq Sy \&? 229character has no effect and is simply ignored. 230.El 231.Pp 232Refer to 233.Xr syslogd 8 234to determine where the system log is written. 235.Pp 236The 237.Fn cmn_err 238function sends log messages to the log of the global zone. 239.Fn cmn_err 240appends a 241.Sy \en 242to each 243.Fa format , 244except when 245.Fa level 246is 247.Sy CE_CONT . 248.Ss Fn dev_err 249With the exception of its first argument 250.Pq Fa dip , 251.Fn dev_err 252is identical to 253.Fn cmn_err . 254.Fa dip 255is a pointer to a device's 256.Ft dev_info 257structure, which is used to prepend the driver name and instance number to the 258message. 259The driver name and instance number are retrieved using 260.Xr ddi_driver_name 9F 261and 262.Xr ddi_get_instance 9F . 263.Ss Fn vcmn_err 264The 265.Fn vcmn_err 266function is identical to 267.Fn cmn_err 268except that its last argument, 269.Fa ap , 270is a pointer to a variable list of arguments. 271.Fa ap 272contains the list of arguments used by the conversion specifications in 273.Fa format . 274.Fa ap 275must be initialized by calling 276.Xr va_start 9F . 277.Xr va_end 9F 278is used to clean up and must be called after each traversal of the list. 279Multiple traversals of the argument list, each bracketed by 280.Xr va_start 9F 281and 282.Xr va_end 9F , 283are possible. 284.Ss Fn vdev_err 285The 286.Fn vdev_err 287function is the combination of 288.Fn vcmn_err 289and 290.Fn dev_err . 291It treats its initial arguments, 292.Fa dip , 293.Fa level , 294and 295.Fa format 296the same as 297.Fn dev_err ; 298however, its last argument 299.Fa ap 300is handled the same way as 301.Fn vcmn_err . 302.Ss Fn zcmn_err 303With the exception of its first argument 304.Pq Fa zoneid , 305.Fn zcmn_err 306is identical to 307.Fn cmn_err . 308.Fa zoneid 309is the numeric ID of the zone to which the message should be directed. 310Note that 311.Fa zoneid 312only has an effect if the message is sent to the system log. 313Using 314.Fa zoneid 315will cause messages to be sent to the log associated with the specified local 316zone rather than the log in the global zone. 317This is accomplished by the message being received and processed by the 318.Xr syslogd 8 319process running in the specified zone instead of the one running in the global 320zone. 321You can retrieve a process zone ID from its credential structure using 322.Xr crgetzoneid 9F . 323.Ss Fn vzcmn_err 324With the exception of its first argument 325.Pq Fa zoneid , 326.Fn vzcmn_err 327is identical to 328.Fn vcmn_err . 329See the description of 330.Fn zcmn_err 331above for an explanation on how the 332.Fa zoneid 333argument is handled. 334.Sh CONTEXT 335The 336.Fn cmn_err , 337.Fn dev_err , 338.Fn vcmn_err , 339.Fn vdev_err , 340.Fn zcmn_err , 341and 342.Fn vzcmn_err 343functions can be called from user, kernel, interrupt, or high-level interrupt 344context. 345.Sh RETURN VALUES 346None. 347However, if an unknown 348.Fa level 349is passed to 350.Fn cmn_err , 351the following panic error message is displayed: 352.Bd -literal 353panic: unknown level in cmn_err (level=level, msg=format) 354.Ed 355.Sh EXAMPLES 356.Bl -tag -width Ds 357.It Sy Example 1 No Using Fn cmn_err 358This first example shows how 359.Fn cmn_err 360can record tracing and debugging information only in the system log 361.Pq lines 17 ; 362display problems with a device only on the system console 363.Pq line 23 ; 364or display problems with the device on both the system console and in the system 365log 366.Pq line 28 . 367.Bd -literal 3681 struct reg { 3692 uchar_t data; 3703 uchar_t csr; 3714 }; 3725 3736 struct xxstate { 3747 ... 3758 dev_info_t *dip; 3769 struct reg *regp; 37710 ... 37811 }; 37912 38013 dev_t dev; 38114 struct xxstate *xsp; 38215 ... 38316 #ifdef DEBUG /* in debugging mode, log function call */ 38417 cmn_err(CE_CONT, "!%s%d: xxopen function called.", 38518 ddi_binding_name(xsp->dip), getminor(dev)); 38619 #endif /* end DEBUG */ 38720 ... 38821 /* display device power failure on system console */ 38922 if ((xsp->regp->csr & POWER) == OFF) 39023 cmn_err(CE_NOTE, "^OFF.", 39124 ddi_binding_name(xsp->dip), getminor(dev)); 39225 ... 39326 /* display warning if device has bad VTOC */ 39427 if (xsp->regp->csr & BADVTOC) 39528 cmn_err(CE_WARN, "%s%d: xxopen: Bad VTOC.", 39629 ddi_binding_name(xsp->dip), getminor(dev)); 397.Ed 398.It Sy Example 2 No Using the %b conversion specification 399This example shows how to use the 400.Sy %b 401conversion specification. 402Because of the leading 403.Qq Sy \&? 404character in the format string, this message will always be logged, but it will 405only be displayed when the kernel is booted in verbose mode. 406.Bd -literal 407cmn_err(CE_CONT, "?reg=0x%b\en", regval, "\e020\e3Intr\e2Err\e1Enable"); 408.Ed 409.It Sy Example 3 No Using Fa regval 410When 411.Fa regval 412is set to 413.Pq decimal 41413, the following message would be displayed: 415.Bd -literal 416reg=0xd<Intr,,Enable> 417.Ed 418.It Sy Example 4 No Error Routine 419This example shows an error reporting routine which accepts a variable number 420of arguments and displays a single line error message both in the system log 421and on the system console. 422Note the use of 423.Fn vsprintf 424to construct the error message before calling 425.Fn cmn_err . 426.Bd -literal 427#include <sys/varargs.h> 428#include <sys/ddi.h> 429#include <sys/sunddi.h> 430#define MAX_MSG 256; 431 432void 433xxerror(dev_info_t *dip, int level, const char *fmt, ...) 434{ 435 va_list ap; 436 int instance; 437 char buf[MAX_MSG], *name; 438 439instance = ddi_get_instance(dip); 440name = ddi_binding_name(dip); 441 442/* format buf using fmt and arguments contained in ap */ 443 444va_start(ap, fmt); 445vsprintf(buf, fmt, ap); 446va_end(ap); 447 448/* pass formatted string to cmn_err(9F) */ 449 450cmn_err(level, "%s%d: %s", name, instance, buf); 451 452} 453.Ed 454.It Sy Example 5 No Log to Current Zone 455This example shows how messages can be sent to the log of the zone in which a 456thread is currently running, when applicable. 457Note that most hardware-related messages should instead be sent to the global 458zone using 459.Fn cmn_err . 460.Bd -literal 461zcmn_err(crgetzoneid(ddi_get_cred()), CE_NOTE, "out of processes"); 462.Ed 463.El 464.Sh SEE ALSO 465.Xr zones 7 , 466.Xr dmesg 8 , 467.Xr kernel 8 , 468.Xr ddi_binding_name 9F , 469.Xr ddi_cred 9F , 470.Xr ddi_driver_name 9F , 471.Xr ddi_get_instance 9F , 472.Xr sprintf 9F , 473.Xr va_arg 9F , 474.Xr va_end 9F , 475.Xr va_start 9F 476.Pp 477Writing Device Drivers 478.Sh WARNINGS 479The 480.Fn cmn_err 481function with the 482.Sy CE_CONT 483argument can be used by driver developers as a driver code debugging tool. 484However, using 485.Fn cmn_err 486in this capacity can change system timing characteristics. 487.Sh NOTES 488Messages of arbitrary length can be generated using 489.Fn cmn_err , 490but if the call to 491.Fn cmn_err 492is made from high-level interrupt context and insufficient memory is available 493to create a buffer of the specified size, the message will be truncated to 494LOG_MSGSIZE 495bytes 496.Pq see Pa sys/log.h . 497For this reason, callers of 498.Fn cmn_err 499that require complete and accurate message generation should post down from 500high-level interrupt context before calling 501.Fn cmn_err . 502