4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
27 * Copyright 2016 Joyent, Inc.
30 #include <sys/param.h>
31 #include <sys/types.h>
32 #include <sys/sysmacros.h>
33 #include <sys/inline.h>
34 #include <sys/varargs.h>
35 #include <sys/systm.h>
37 #include <sys/cmn_err.h>
38 #include <sys/syslog.h>
41 #include <sys/vnode.h>
42 #include <sys/session.h>
43 #include <sys/stream.h>
46 #include <sys/atomic.h>
47 #include <sys/console.h>
48 #include <sys/cpuvar.h>
49 #include <sys/modctl.h>
50 #include <sys/reboot.h>
51 #include <sys/debug.h>
52 #include <sys/panic.h>
55 #include <sys/sunddi.h>
58 * In some debugging situations it's useful to log all messages to panicbuf,
59 * which is persistent across reboots (on most platforms). The range
60 * panicbuf[panicbuf_log..PANICBUFSIZE-1] may be used for this purpose.
61 * By default, panicbuf_log == PANICBUFSIZE and no messages are logged.
62 * To enable panicbuf logging, set panicbuf_log to a small value, say 1K;
63 * this will reserve 1K for panic information and 7K for message logging.
65 uint32_t panicbuf_log
= PANICBUFSIZE
;
66 uint32_t panicbuf_index
= PANICBUFSIZE
;
69 static int ce_to_sl
[CE_IGNORE
] = { SL_NOTE
, SL_NOTE
, SL_WARN
, SL_FATAL
};
70 static char ce_prefix
[CE_IGNORE
][10] = { "", "NOTICE: ", "WARNING: ", "" };
71 static char ce_suffix
[CE_IGNORE
][2] = { "", "\n", "\n", "" };
74 cprintf(const char *fmt
, va_list adx
, int sl
, const char *prefix
,
75 const char *suffix
, void *site
, int mid
, int sid
, int level
,
76 zoneid_t zoneid
, dev_info_t
*dip
)
79 size_t bufsize
= LOG_MSGSIZE
;
80 char buf
[LOG_MSGSIZE
];
82 char *body
, *msgp
, *bufend
;
88 on_intr
= CPU_ON_INTR(CPU
) ||
89 (interrupts_unleashed
&& (spltoipl(s
) > LOCK_LEVEL
));
92 ASSERT(zoneid
== GLOBAL_ZONEID
|| !on_intr
);
94 STRLOG_MAKE_MSGID(fmt
, msgid
);
96 if (strchr("^!?", fmt
[0]) != NULL
) {
99 else if (fmt
[0] == '!' ||
100 (prefix
[0] == '\0' && !(boothowto
& RB_VERBOSE
)))
101 sl
= (sl
& ~(SL_USER
| SL_NOTE
)) | SL_LOGONLY
;
105 if ((sl
& SL_USER
) && (MUTEX_HELD(&pidlock
) || on_intr
)) {
106 zoneid
= getzoneid();
107 sl
= sl
& ~(SL_USER
| SL_LOGONLY
) | SL_CONSOLE
;
111 bufend
= bufp
+ bufsize
;
113 body
= msgp
+= snprintf(msgp
, bufend
- msgp
,
114 "%s: [ID %u FACILITY_AND_PRIORITY] ",
115 mod_containing_pc(site
), msgid
);
116 msgp
+= snprintf(msgp
, bufend
- msgp
, prefix
);
118 msgp
+= snprintf(msgp
, bufend
- msgp
, "%s%d: ",
119 ddi_driver_name(dip
), ddi_get_instance(dip
));
120 msgp
+= vsnprintf(msgp
, bufend
- msgp
, fmt
, adx
);
121 msgp
+= snprintf(msgp
, bufend
- msgp
, suffix
);
124 if (((sl
& SL_CONSONLY
) && panicstr
) ||
125 (zoneid
== GLOBAL_ZONEID
&& log_global
.lz_active
== 0)) {
126 console_printf("%s", body
);
130 if (msgp
- bufp
>= bufsize
&& !on_intr
) {
132 bufsize
= msgp
- bufp
+ 1;
133 bufp
= kmem_alloc(bufsize
, KM_NOSLEEP
);
136 bufsize
= LOG_MSGSIZE
;
140 mp
= log_makemsg(mid
, sid
, level
, sl
, LOG_KERN
, bufp
,
141 MIN(bufsize
, msgp
- bufp
+ 1), on_intr
);
143 if ((sl
& (SL_CONSOLE
| SL_LOGONLY
)) == SL_CONSOLE
&& !on_intr
)
144 console_printf("%s", body
);
152 if ((sp
= tty_hold()) != NULL
) {
153 if (sp
->s_vp
!= NULL
)
154 (void) vn_rdwr(UIO_WRITE
, sp
->s_vp
, body
,
155 len
, 0LL, UIO_SYSSPACE
, FAPPEND
,
156 (rlim64_t
)LOG_HIWAT
, kcred
, &resid
);
161 if (on_intr
&& !panicstr
) {
162 (void) putq(log_intrq
, mp
);
163 softcall((void (*)(void *))log_flushq
, log_intrq
);
165 log_sendmsg(mp
, zoneid
);
168 if (panicbuf_log
+ len
< PANICBUFSIZE
) {
171 old
= panicbuf_index
;
173 if (new >= PANICBUFSIZE
)
174 new = panicbuf_log
+ len
;
175 } while (atomic_cas_32(&panicbuf_index
, old
, new) != old
);
176 bcopy(body
, &panicbuf
[new - len
], len
);
179 kmem_free(bufp
, bufsize
);
183 vzprintf(zoneid_t zoneid
, const char *fmt
, va_list adx
)
185 cprintf(fmt
, adx
, SL_CONSOLE
| SL_NOTE
, "", "", caller(), 0, 0, 0,
190 vprintf(const char *fmt
, va_list adx
)
192 vzprintf(GLOBAL_ZONEID
, fmt
, adx
);
197 printf(const char *fmt
, ...)
202 cprintf(fmt
, adx
, SL_CONSOLE
| SL_NOTE
, "", "", caller(), 0, 0, 0,
203 GLOBAL_ZONEID
, NULL
);
209 zprintf(zoneid_t zoneid
, const char *fmt
, ...)
214 cprintf(fmt
, adx
, SL_CONSOLE
| SL_NOTE
, "", "", caller(), 0, 0, 0,
220 vuprintf(const char *fmt
, va_list adx
)
225 /* Message the user tty, if any, and the global zone syslog */
226 cprintf(fmt
, adx
, SL_CONSOLE
| SL_LOGONLY
| SL_USER
| SL_NOTE
,
227 "", "", caller(), 0, 0, 0, GLOBAL_ZONEID
, NULL
);
229 /* Now message the local zone syslog */
230 if (!INGLOBALZONE(curproc
))
231 cprintf(fmt
, adxcp
, SL_CONSOLE
| SL_LOGONLY
| SL_NOTE
,
232 "", "", caller(), 0, 0, 0, getzoneid(), NULL
);
239 uprintf(const char *fmt
, ...)
251 vzdcmn_err(zoneid_t zoneid
, void *site
, int ce
, const char *fmt
, va_list adx
,
256 if ((uint_t
)ce
< CE_IGNORE
) {
257 cprintf(fmt
, adx
, ce_to_sl
[ce
] | SL_CONSOLE
,
258 ce_prefix
[ce
], ce_suffix
[ce
], site
, 0, 0, 0,
264 vzcmn_err(zoneid_t zoneid
, int ce
, const char *fmt
, va_list adx
)
266 vzdcmn_err(zoneid
, caller(), ce
, fmt
, adx
, NULL
);
270 vcmn_err(int ce
, const char *fmt
, va_list adx
)
272 vzdcmn_err(GLOBAL_ZONEID
, caller(), ce
, fmt
, adx
, NULL
);
277 vdev_err(dev_info_t
*dip
, int ce
, const char *fmt
, va_list adx
)
279 vzdcmn_err(GLOBAL_ZONEID
, caller(), ce
, fmt
, adx
, dip
);
284 cmn_err(int ce
, const char *fmt
, ...)
289 vzdcmn_err(GLOBAL_ZONEID
, caller(), ce
, fmt
, adx
, NULL
);
295 zcmn_err(zoneid_t zoneid
, int ce
, const char *fmt
, ...)
300 vzdcmn_err(zoneid
, caller(), ce
, fmt
, adx
, NULL
);
306 dev_err(dev_info_t
*dip
, int ce
, char *fmt
, ...)
311 vzdcmn_err(GLOBAL_ZONEID
, caller(), ce
, fmt
, adx
, dip
);
316 assfail(const char *a
, const char *f
, int l
)
319 printf("ASSERTION CAUGHT: %s, file: %s, line: %d", a
, f
, l
);
323 if (!aok
&& !panicstr
)
324 panic("assertion failed: %s, file: %s, line: %d", a
, f
, l
);
330 assfail3(const char *a
, uintmax_t lv
, const char *op
, uintmax_t rv
,
331 const char *f
, int l
)
334 printf("ASSERTION CAUGHT: %s (0x%llx %s 0x%llx), file: %s, "
335 "line: %d", a
, (u_longlong_t
)lv
, op
, (u_longlong_t
)rv
,
340 if (!aok
&& !panicstr
)
341 panic("assertion failed: %s (0x%llx %s 0x%llx), file: %s, "
342 "line: %d", a
, (u_longlong_t
)lv
, op
, (u_longlong_t
)rv
,
347 strlog(short mid
, short sid
, char level
, ushort_t sl
, char *fmt
, ...)
349 if (sl
& log_global
.lz_active
) {
352 cprintf(fmt
, adx
, sl
, "", "", caller(), mid
, sid
, level
,
353 GLOBAL_ZONEID
, NULL
);
360 vstrlog(short mid
, short sid
, char level
, ushort_t sl
, char *fmt
, va_list adx
)
362 if (sl
& log_global
.lz_active
)
363 cprintf(fmt
, adx
, sl
, "", "", caller(), mid
, sid
, level
,
364 GLOBAL_ZONEID
, NULL
);