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]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
25 * Copyright 2012 Nexenta Systems, Inc. All rights reserved.
28 #include <sys/param.h>
29 #include <sys/types.h>
30 #include <sys/sysmacros.h>
31 #include <sys/inline.h>
32 #include <sys/varargs.h>
33 #include <sys/systm.h>
35 #include <sys/cmn_err.h>
36 #include <sys/syslog.h>
39 #include <sys/vnode.h>
40 #include <sys/session.h>
41 #include <sys/stream.h>
44 #include <sys/atomic.h>
45 #include <sys/console.h>
46 #include <sys/cpuvar.h>
47 #include <sys/modctl.h>
48 #include <sys/reboot.h>
49 #include <sys/debug.h>
50 #include <sys/panic.h>
53 #include <sys/sunddi.h>
56 * In some debugging situations it's useful to log all messages to panicbuf,
57 * which is persistent across reboots (on most platforms). The range
58 * panicbuf[panicbuf_log..PANICBUFSIZE-1] may be used for this purpose.
59 * By default, panicbuf_log == PANICBUFSIZE and no messages are logged.
60 * To enable panicbuf logging, set panicbuf_log to a small value, say 1K;
61 * this will reserve 1K for panic information and 7K for message logging.
63 uint32_t panicbuf_log
= PANICBUFSIZE
;
64 uint32_t panicbuf_index
= PANICBUFSIZE
;
67 static int ce_to_sl
[CE_IGNORE
] = { SL_NOTE
, SL_NOTE
, SL_WARN
, SL_FATAL
};
68 static char ce_prefix
[CE_IGNORE
][10] = { "", "NOTICE: ", "WARNING: ", "" };
69 static char ce_suffix
[CE_IGNORE
][2] = { "", "\n", "\n", "" };
72 cprintf(const char *fmt
, va_list adx
, int sl
, const char *prefix
,
73 const char *suffix
, void *site
, int mid
, int sid
, int level
,
77 size_t bufsize
= LOG_MSGSIZE
;
78 char buf
[LOG_MSGSIZE
];
80 char *body
, *msgp
, *bufend
;
86 on_intr
= CPU_ON_INTR(CPU
) ||
87 (interrupts_unleashed
&& (spltoipl(s
) > LOCK_LEVEL
));
90 ASSERT(zoneid
== GLOBAL_ZONEID
|| !on_intr
);
92 STRLOG_MAKE_MSGID(fmt
, msgid
);
94 if (strchr("^!?", fmt
[0]) != NULL
) {
97 else if (fmt
[0] == '!' ||
98 (prefix
[0] == '\0' && !(boothowto
& RB_VERBOSE
)))
99 sl
= (sl
& ~(SL_USER
| SL_NOTE
)) | SL_LOGONLY
;
103 if ((sl
& SL_USER
) && (MUTEX_HELD(&pidlock
) || on_intr
)) {
104 zoneid
= getzoneid();
105 sl
= sl
& ~(SL_USER
| SL_LOGONLY
) | SL_CONSOLE
;
109 bufend
= bufp
+ bufsize
;
111 body
= msgp
+= snprintf(msgp
, bufend
- msgp
,
112 "%s: [ID %u FACILITY_AND_PRIORITY] ",
113 mod_containing_pc(site
), msgid
);
114 msgp
+= snprintf(msgp
, bufend
- msgp
, prefix
);
115 msgp
+= vsnprintf(msgp
, bufend
- msgp
, fmt
, adx
);
116 msgp
+= snprintf(msgp
, bufend
- msgp
, suffix
);
119 if (((sl
& SL_CONSONLY
) && panicstr
) ||
120 (zoneid
== GLOBAL_ZONEID
&& log_global
.lz_active
== 0)) {
121 console_printf("%s", body
);
125 if (msgp
- bufp
>= bufsize
&& !on_intr
) {
127 bufsize
= msgp
- bufp
+ 1;
128 bufp
= kmem_alloc(bufsize
, KM_NOSLEEP
);
131 bufsize
= LOG_MSGSIZE
;
135 mp
= log_makemsg(mid
, sid
, level
, sl
, LOG_KERN
, bufp
,
136 MIN(bufsize
, msgp
- bufp
+ 1), on_intr
);
138 if ((sl
& (SL_CONSOLE
| SL_LOGONLY
)) == SL_CONSOLE
&& !on_intr
)
139 console_printf("%s", body
);
147 if ((sp
= tty_hold()) != NULL
) {
148 if (sp
->s_vp
!= NULL
)
149 (void) vn_rdwr(UIO_WRITE
, sp
->s_vp
, body
,
150 len
, 0LL, UIO_SYSSPACE
, FAPPEND
,
151 (rlim64_t
)LOG_HIWAT
, kcred
, &resid
);
156 if (on_intr
&& !panicstr
) {
157 (void) putq(log_intrq
, mp
);
158 softcall((void (*)(void *))log_flushq
, log_intrq
);
160 log_sendmsg(mp
, zoneid
);
163 if (panicbuf_log
+ len
< PANICBUFSIZE
) {
166 old
= panicbuf_index
;
168 if (new >= PANICBUFSIZE
)
169 new = panicbuf_log
+ len
;
170 } while (atomic_cas_32(&panicbuf_index
, old
, new) != old
);
171 bcopy(body
, &panicbuf
[new - len
], len
);
174 kmem_free(bufp
, bufsize
);
178 vzprintf(zoneid_t zoneid
, const char *fmt
, va_list adx
)
180 cprintf(fmt
, adx
, SL_CONSOLE
| SL_NOTE
, "", "", caller(), 0, 0, 0,
185 vprintf(const char *fmt
, va_list adx
)
187 vzprintf(GLOBAL_ZONEID
, fmt
, adx
);
192 printf(const char *fmt
, ...)
197 cprintf(fmt
, adx
, SL_CONSOLE
| SL_NOTE
, "", "", caller(), 0, 0, 0,
204 zprintf(zoneid_t zoneid
, const char *fmt
, ...)
209 cprintf(fmt
, adx
, SL_CONSOLE
| SL_NOTE
, "", "", caller(), 0, 0, 0,
215 vuprintf(const char *fmt
, va_list adx
)
220 /* Message the user tty, if any, and the global zone syslog */
221 cprintf(fmt
, adx
, SL_CONSOLE
| SL_LOGONLY
| SL_USER
| SL_NOTE
,
222 "", "", caller(), 0, 0, 0, GLOBAL_ZONEID
);
224 /* Now message the local zone syslog */
225 if (!INGLOBALZONE(curproc
))
226 cprintf(fmt
, adxcp
, SL_CONSOLE
| SL_LOGONLY
| SL_NOTE
,
227 "", "", caller(), 0, 0, 0, getzoneid());
234 uprintf(const char *fmt
, ...)
246 vzcmn_err(zoneid_t zoneid
, int ce
, const char *fmt
, va_list adx
)
250 if ((uint_t
)ce
< CE_IGNORE
)
251 cprintf(fmt
, adx
, ce_to_sl
[ce
] | SL_CONSOLE
,
252 ce_prefix
[ce
], ce_suffix
[ce
], caller(), 0, 0, 0,
257 vcmn_err(int ce
, const char *fmt
, va_list adx
)
259 vzcmn_err(GLOBAL_ZONEID
, ce
, fmt
, adx
);
264 cmn_err(int ce
, const char *fmt
, ...)
271 if ((uint_t
)ce
< CE_IGNORE
)
272 cprintf(fmt
, adx
, ce_to_sl
[ce
] | SL_CONSOLE
,
273 ce_prefix
[ce
], ce_suffix
[ce
], caller(), 0, 0, 0,
280 zcmn_err(zoneid_t zoneid
, int ce
, const char *fmt
, ...)
287 if ((uint_t
)ce
< CE_IGNORE
)
288 cprintf(fmt
, adx
, ce_to_sl
[ce
] | SL_CONSOLE
, ce_prefix
[ce
],
289 ce_suffix
[ce
], caller(), 0, 0, 0, zoneid
);
295 dev_err(dev_info_t
*dip
, int ce
, char *fmt
, ...)
298 char buf
[LOG_MSGSIZE
];
300 (void) snprintf(buf
, sizeof (buf
), "%s%d: %s",
301 ddi_driver_name(dip
), ddi_get_instance(dip
), fmt
);
304 vcmn_err(ce
, buf
, ap
);
309 assfail(const char *a
, const char *f
, int l
)
312 printf("ASSERTION CAUGHT: %s, file: %s, line: %d", a
, f
, l
);
316 if (!aok
&& !panicstr
)
317 panic("assertion failed: %s, file: %s, line: %d", a
, f
, l
);
323 assfail3(const char *a
, uintmax_t lv
, const char *op
, uintmax_t rv
,
324 const char *f
, int l
)
327 printf("ASSERTION CAUGHT: %s (0x%llx %s 0x%llx), file: %s, "
328 "line: %d", a
, (u_longlong_t
)lv
, op
, (u_longlong_t
)rv
,
333 if (!aok
&& !panicstr
)
334 panic("assertion failed: %s (0x%llx %s 0x%llx), file: %s, "
335 "line: %d", a
, (u_longlong_t
)lv
, op
, (u_longlong_t
)rv
,
340 strlog(short mid
, short sid
, char level
, ushort_t sl
, char *fmt
, ...)
342 if (sl
& log_global
.lz_active
) {
345 cprintf(fmt
, adx
, sl
, "", "", caller(), mid
, sid
, level
,
353 vstrlog(short mid
, short sid
, char level
, ushort_t sl
, char *fmt
, va_list adx
)
355 if (sl
& log_global
.lz_active
)
356 cprintf(fmt
, adx
, sl
, "", "", caller(), mid
, sid
, level
,