xref: /onnv-gate/usr/src/uts/common/os/printf.c (revision 9613:26c5867ec56a)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52712Snn35248  * Common Development and Distribution License (the "License").
62712Snn35248  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*9613SAbhinandan.Ekande@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/param.h>
270Sstevel@tonic-gate #include <sys/types.h>
280Sstevel@tonic-gate #include <sys/sysmacros.h>
290Sstevel@tonic-gate #include <sys/inline.h>
300Sstevel@tonic-gate #include <sys/varargs.h>
310Sstevel@tonic-gate #include <sys/systm.h>
320Sstevel@tonic-gate #include <sys/conf.h>
330Sstevel@tonic-gate #include <sys/cmn_err.h>
340Sstevel@tonic-gate #include <sys/syslog.h>
350Sstevel@tonic-gate #include <sys/log.h>
360Sstevel@tonic-gate #include <sys/proc.h>
370Sstevel@tonic-gate #include <sys/vnode.h>
380Sstevel@tonic-gate #include <sys/session.h>
390Sstevel@tonic-gate #include <sys/stream.h>
400Sstevel@tonic-gate #include <sys/kmem.h>
410Sstevel@tonic-gate #include <sys/kobj.h>
420Sstevel@tonic-gate #include <sys/atomic.h>
430Sstevel@tonic-gate #include <sys/console.h>
440Sstevel@tonic-gate #include <sys/cpuvar.h>
450Sstevel@tonic-gate #include <sys/modctl.h>
460Sstevel@tonic-gate #include <sys/reboot.h>
470Sstevel@tonic-gate #include <sys/debug.h>
480Sstevel@tonic-gate #include <sys/panic.h>
490Sstevel@tonic-gate #include <sys/spl.h>
500Sstevel@tonic-gate #include <sys/zone.h>
510Sstevel@tonic-gate 
520Sstevel@tonic-gate /*
530Sstevel@tonic-gate  * In some debugging situations it's useful to log all messages to panicbuf,
540Sstevel@tonic-gate  * which is persistent across reboots (on most platforms).  The range
550Sstevel@tonic-gate  * panicbuf[panicbuf_log..PANICBUFSIZE-1] may be used for this purpose.
560Sstevel@tonic-gate  * By default, panicbuf_log == PANICBUFSIZE and no messages are logged.
570Sstevel@tonic-gate  * To enable panicbuf logging, set panicbuf_log to a small value, say 1K;
580Sstevel@tonic-gate  * this will reserve 1K for panic information and 7K for message logging.
590Sstevel@tonic-gate  */
600Sstevel@tonic-gate uint32_t panicbuf_log = PANICBUFSIZE;
610Sstevel@tonic-gate uint32_t panicbuf_index = PANICBUFSIZE;
620Sstevel@tonic-gate 
630Sstevel@tonic-gate static int aask, aok;
640Sstevel@tonic-gate static int ce_to_sl[CE_IGNORE] = { SL_NOTE, SL_NOTE, SL_WARN, SL_FATAL };
650Sstevel@tonic-gate static char ce_prefix[CE_IGNORE][10] = { "", "NOTICE: ", "WARNING: ", "" };
660Sstevel@tonic-gate static char ce_suffix[CE_IGNORE][2] = { "", "\n", "\n", "" };
670Sstevel@tonic-gate 
680Sstevel@tonic-gate static void
cprintf(const char * fmt,va_list adx,int sl,const char * prefix,const char * suffix,void * site,int mid,int sid,int level,zoneid_t zoneid)690Sstevel@tonic-gate cprintf(const char *fmt, va_list adx, int sl, const char *prefix,
700Sstevel@tonic-gate 	const char *suffix, void *site, int mid, int sid, int level,
710Sstevel@tonic-gate 	zoneid_t zoneid)
720Sstevel@tonic-gate {
730Sstevel@tonic-gate 	uint32_t msgid;
740Sstevel@tonic-gate 	size_t bufsize = LOG_MSGSIZE;
750Sstevel@tonic-gate 	char buf[LOG_MSGSIZE];
760Sstevel@tonic-gate 	char *bufp = buf;
770Sstevel@tonic-gate 	char *body, *msgp, *bufend;
780Sstevel@tonic-gate 	mblk_t *mp;
790Sstevel@tonic-gate 	int s, on_intr;
800Sstevel@tonic-gate 	size_t len;
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	s = splhi();
830Sstevel@tonic-gate 	on_intr = CPU_ON_INTR(CPU) ||
840Sstevel@tonic-gate 	    (interrupts_unleashed && (spltoipl(s) > LOCK_LEVEL));
850Sstevel@tonic-gate 	splx(s);
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	ASSERT(zoneid == GLOBAL_ZONEID || !on_intr);
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	STRLOG_MAKE_MSGID(fmt, msgid);
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	if (strchr("^!?", fmt[0]) != NULL) {
920Sstevel@tonic-gate 		if (fmt[0] == '^')
930Sstevel@tonic-gate 			sl |= SL_CONSONLY;
940Sstevel@tonic-gate 		else if (fmt[0] == '!' ||
950Sstevel@tonic-gate 		    (prefix[0] == '\0' && !(boothowto & RB_VERBOSE)))
960Sstevel@tonic-gate 			sl = (sl & ~(SL_USER | SL_NOTE)) | SL_LOGONLY;
970Sstevel@tonic-gate 		fmt++;
980Sstevel@tonic-gate 	}
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	if ((sl & SL_USER) && (MUTEX_HELD(&pidlock) || on_intr)) {
1010Sstevel@tonic-gate 		zoneid = getzoneid();
1020Sstevel@tonic-gate 		sl = sl & ~(SL_USER | SL_LOGONLY) | SL_CONSOLE;
1030Sstevel@tonic-gate 	}
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate retry:
1060Sstevel@tonic-gate 	bufend = bufp + bufsize;
1070Sstevel@tonic-gate 	msgp = bufp;
1080Sstevel@tonic-gate 	body = msgp += snprintf(msgp, bufend - msgp,
1090Sstevel@tonic-gate 	    "%s: [ID %u FACILITY_AND_PRIORITY] ",
1100Sstevel@tonic-gate 	    mod_containing_pc(site), msgid);
1110Sstevel@tonic-gate 	msgp += snprintf(msgp, bufend - msgp, prefix);
1120Sstevel@tonic-gate 	msgp += vsnprintf(msgp, bufend - msgp, fmt, adx);
1130Sstevel@tonic-gate 	msgp += snprintf(msgp, bufend - msgp, suffix);
1140Sstevel@tonic-gate 	len = strlen(body);
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	if (((sl & SL_CONSONLY) && panicstr) ||
1170Sstevel@tonic-gate 	    (zoneid == GLOBAL_ZONEID && log_global.lz_active == 0)) {
1180Sstevel@tonic-gate 		console_printf("%s", body);
1190Sstevel@tonic-gate 		goto out;
1200Sstevel@tonic-gate 	}
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	if (msgp - bufp >= bufsize && !on_intr) {
1230Sstevel@tonic-gate 		ASSERT(bufp == buf);
1240Sstevel@tonic-gate 		bufsize = msgp - bufp + 1;
1250Sstevel@tonic-gate 		bufp = kmem_alloc(bufsize, KM_NOSLEEP);
1260Sstevel@tonic-gate 		if (bufp != NULL)
1270Sstevel@tonic-gate 			goto retry;
1280Sstevel@tonic-gate 		bufsize = LOG_MSGSIZE;
1290Sstevel@tonic-gate 		bufp = buf;
1300Sstevel@tonic-gate 	}
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	mp = log_makemsg(mid, sid, level, sl, LOG_KERN, bufp,
1330Sstevel@tonic-gate 	    MIN(bufsize, msgp - bufp + 1), on_intr);
1340Sstevel@tonic-gate 	if (mp == NULL) {
1350Sstevel@tonic-gate 		if ((sl & (SL_CONSOLE | SL_LOGONLY)) == SL_CONSOLE && !on_intr)
1360Sstevel@tonic-gate 			console_printf("%s", body);
1370Sstevel@tonic-gate 		goto out;
1380Sstevel@tonic-gate 	}
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	if (sl & SL_USER) {
1410Sstevel@tonic-gate 		ssize_t resid;
1422712Snn35248 		sess_t *sp;
1430Sstevel@tonic-gate 
1442712Snn35248 		if ((sp = tty_hold()) != NULL) {
1452712Snn35248 			if (sp->s_vp != NULL)
1462712Snn35248 				(void) vn_rdwr(UIO_WRITE, sp->s_vp, body,
1472712Snn35248 				    len, 0LL, UIO_SYSSPACE, FAPPEND,
1482712Snn35248 				    (rlim64_t)LOG_HIWAT, kcred, &resid);
1492712Snn35248 			tty_rele(sp);
1502712Snn35248 		}
1510Sstevel@tonic-gate 	}
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	if (on_intr && !panicstr) {
1540Sstevel@tonic-gate 		(void) putq(log_intrq, mp);
1550Sstevel@tonic-gate 		softcall((void (*)(void *))log_flushq, log_intrq);
1560Sstevel@tonic-gate 	} else {
1570Sstevel@tonic-gate 		log_sendmsg(mp, zoneid);
1580Sstevel@tonic-gate 	}
1590Sstevel@tonic-gate out:
1600Sstevel@tonic-gate 	if (panicbuf_log + len < PANICBUFSIZE) {
1610Sstevel@tonic-gate 		uint32_t old, new;
1620Sstevel@tonic-gate 		do {
1630Sstevel@tonic-gate 			old = panicbuf_index;
1640Sstevel@tonic-gate 			new = old + len;
1650Sstevel@tonic-gate 			if (new >= PANICBUFSIZE)
1660Sstevel@tonic-gate 				new = panicbuf_log + len;
1670Sstevel@tonic-gate 		} while (cas32(&panicbuf_index, old, new) != old);
1680Sstevel@tonic-gate 		bcopy(body, &panicbuf[new - len], len);
1690Sstevel@tonic-gate 	}
1700Sstevel@tonic-gate 	if (bufp != buf)
1710Sstevel@tonic-gate 		kmem_free(bufp, bufsize);
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate void
vzprintf(zoneid_t zoneid,const char * fmt,va_list adx)1750Sstevel@tonic-gate vzprintf(zoneid_t zoneid, const char *fmt, va_list adx)
1760Sstevel@tonic-gate {
1770Sstevel@tonic-gate 	cprintf(fmt, adx, SL_CONSOLE | SL_NOTE, "", "", caller(), 0, 0, 0,
1780Sstevel@tonic-gate 	    zoneid);
1790Sstevel@tonic-gate }
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate void
vprintf(const char * fmt,va_list adx)1820Sstevel@tonic-gate vprintf(const char *fmt, va_list adx)
1830Sstevel@tonic-gate {
1840Sstevel@tonic-gate 	vzprintf(GLOBAL_ZONEID, fmt, adx);
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate /*PRINTFLIKE1*/
1880Sstevel@tonic-gate void
printf(const char * fmt,...)1890Sstevel@tonic-gate printf(const char *fmt, ...)
1900Sstevel@tonic-gate {
1910Sstevel@tonic-gate 	va_list adx;
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	va_start(adx, fmt);
1940Sstevel@tonic-gate 	cprintf(fmt, adx, SL_CONSOLE | SL_NOTE, "", "", caller(), 0, 0, 0,
1950Sstevel@tonic-gate 	    GLOBAL_ZONEID);
1960Sstevel@tonic-gate 	va_end(adx);
1970Sstevel@tonic-gate }
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate /*PRINTFLIKE2*/
2000Sstevel@tonic-gate void
zprintf(zoneid_t zoneid,const char * fmt,...)2010Sstevel@tonic-gate zprintf(zoneid_t zoneid, const char *fmt, ...)
2020Sstevel@tonic-gate {
2030Sstevel@tonic-gate 	va_list adx;
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	va_start(adx, fmt);
2060Sstevel@tonic-gate 	cprintf(fmt, adx, SL_CONSOLE | SL_NOTE, "", "", caller(), 0, 0, 0,
2070Sstevel@tonic-gate 	    zoneid);
2080Sstevel@tonic-gate 	va_end(adx);
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate void
vuprintf(const char * fmt,va_list adx)2120Sstevel@tonic-gate vuprintf(const char *fmt, va_list adx)
2130Sstevel@tonic-gate {
214*9613SAbhinandan.Ekande@Sun.COM 	va_list adxcp;
215*9613SAbhinandan.Ekande@Sun.COM 	va_copy(adxcp, adx);
216*9613SAbhinandan.Ekande@Sun.COM 
217*9613SAbhinandan.Ekande@Sun.COM 	/* Message the user tty, if any, and the global zone syslog */
2180Sstevel@tonic-gate 	cprintf(fmt, adx, SL_CONSOLE | SL_LOGONLY | SL_USER | SL_NOTE,
2190Sstevel@tonic-gate 	    "", "", caller(), 0, 0, 0, GLOBAL_ZONEID);
220*9613SAbhinandan.Ekande@Sun.COM 
221*9613SAbhinandan.Ekande@Sun.COM 	/* Now message the local zone syslog */
222*9613SAbhinandan.Ekande@Sun.COM 	if (!INGLOBALZONE(curproc))
223*9613SAbhinandan.Ekande@Sun.COM 		cprintf(fmt, adxcp, SL_CONSOLE | SL_LOGONLY | SL_NOTE,
224*9613SAbhinandan.Ekande@Sun.COM 		    "", "", caller(), 0, 0, 0, getzoneid());
225*9613SAbhinandan.Ekande@Sun.COM 
226*9613SAbhinandan.Ekande@Sun.COM 	va_end(adxcp);
2270Sstevel@tonic-gate }
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate /*PRINTFLIKE1*/
2300Sstevel@tonic-gate void
uprintf(const char * fmt,...)2310Sstevel@tonic-gate uprintf(const char *fmt, ...)
2320Sstevel@tonic-gate {
2330Sstevel@tonic-gate 	va_list adx;
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	va_start(adx, fmt);
236*9613SAbhinandan.Ekande@Sun.COM 
237*9613SAbhinandan.Ekande@Sun.COM 	vuprintf(fmt, adx);
238*9613SAbhinandan.Ekande@Sun.COM 
2390Sstevel@tonic-gate 	va_end(adx);
2400Sstevel@tonic-gate }
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate void
vzcmn_err(zoneid_t zoneid,int ce,const char * fmt,va_list adx)2430Sstevel@tonic-gate vzcmn_err(zoneid_t zoneid, int ce, const char *fmt, va_list adx)
2440Sstevel@tonic-gate {
2450Sstevel@tonic-gate 	if (ce == CE_PANIC)
2460Sstevel@tonic-gate 		vpanic(fmt, adx);
2470Sstevel@tonic-gate 	if ((uint_t)ce < CE_IGNORE)
2480Sstevel@tonic-gate 		cprintf(fmt, adx, ce_to_sl[ce] | SL_CONSOLE,
2490Sstevel@tonic-gate 		    ce_prefix[ce], ce_suffix[ce], caller(), 0, 0, 0,
2500Sstevel@tonic-gate 		    zoneid);
2510Sstevel@tonic-gate }
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate void
vcmn_err(int ce,const char * fmt,va_list adx)2540Sstevel@tonic-gate vcmn_err(int ce, const char *fmt, va_list adx)
2550Sstevel@tonic-gate {
2560Sstevel@tonic-gate 	vzcmn_err(GLOBAL_ZONEID, ce, fmt, adx);
2570Sstevel@tonic-gate }
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate /*PRINTFLIKE2*/
2600Sstevel@tonic-gate void
cmn_err(int ce,const char * fmt,...)2610Sstevel@tonic-gate cmn_err(int ce, const char *fmt, ...)
2620Sstevel@tonic-gate {
2630Sstevel@tonic-gate 	va_list adx;
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 	va_start(adx, fmt);
2660Sstevel@tonic-gate 	if (ce == CE_PANIC)
2670Sstevel@tonic-gate 		vpanic(fmt, adx);
2680Sstevel@tonic-gate 	if ((uint_t)ce < CE_IGNORE)
2690Sstevel@tonic-gate 		cprintf(fmt, adx, ce_to_sl[ce] | SL_CONSOLE,
2700Sstevel@tonic-gate 		    ce_prefix[ce], ce_suffix[ce], caller(), 0, 0, 0,
2710Sstevel@tonic-gate 		    GLOBAL_ZONEID);
2720Sstevel@tonic-gate 	va_end(adx);
2730Sstevel@tonic-gate }
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate /*PRINTFLIKE3*/
2760Sstevel@tonic-gate void
zcmn_err(zoneid_t zoneid,int ce,const char * fmt,...)2770Sstevel@tonic-gate zcmn_err(zoneid_t zoneid, int ce, const char *fmt, ...)
2780Sstevel@tonic-gate {
2790Sstevel@tonic-gate 	va_list adx;
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	va_start(adx, fmt);
2820Sstevel@tonic-gate 	if (ce == CE_PANIC)
2830Sstevel@tonic-gate 		vpanic(fmt, adx);
2840Sstevel@tonic-gate 	if ((uint_t)ce < CE_IGNORE)
2850Sstevel@tonic-gate 		cprintf(fmt, adx, ce_to_sl[ce] | SL_CONSOLE, ce_prefix[ce],
2860Sstevel@tonic-gate 		    ce_suffix[ce], caller(), 0, 0, 0, zoneid);
2870Sstevel@tonic-gate 	va_end(adx);
2880Sstevel@tonic-gate }
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate int
assfail(const char * a,const char * f,int l)2910Sstevel@tonic-gate assfail(const char *a, const char *f, int l)
2920Sstevel@tonic-gate {
2930Sstevel@tonic-gate 	if (aask)  {
2940Sstevel@tonic-gate 		printf("ASSERTION CAUGHT: %s, file: %s, line: %d", a, f, l);
2950Sstevel@tonic-gate 		debug_enter(NULL);
2960Sstevel@tonic-gate 	}
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	if (!aok && !panicstr)
2990Sstevel@tonic-gate 		panic("assertion failed: %s, file: %s, line: %d", a, f, l);
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 	return (0);
3020Sstevel@tonic-gate }
3030Sstevel@tonic-gate 
304789Sahrens void
assfail3(const char * a,uintmax_t lv,const char * op,uintmax_t rv,const char * f,int l)305789Sahrens assfail3(const char *a, uintmax_t lv, const char *op, uintmax_t rv,
306789Sahrens     const char *f, int l)
307789Sahrens {
308789Sahrens 	if (aask)  {
309789Sahrens 		printf("ASSERTION CAUGHT: %s (0x%llx %s 0x%llx), file: %s, "
310789Sahrens 		    "line: %d", a, (u_longlong_t)lv, op, (u_longlong_t)rv,
311789Sahrens 		    f, l);
312789Sahrens 		debug_enter(NULL);
313789Sahrens 	}
314789Sahrens 
315789Sahrens 	if (!aok && !panicstr)
316789Sahrens 		panic("assertion failed: %s (0x%llx %s 0x%llx), file: %s, "
317789Sahrens 		    "line: %d", a, (u_longlong_t)lv, op, (u_longlong_t)rv,
318789Sahrens 		    f, l);
319789Sahrens }
320789Sahrens 
3210Sstevel@tonic-gate int
strlog(short mid,short sid,char level,ushort_t sl,char * fmt,...)3220Sstevel@tonic-gate strlog(short mid, short sid, char level, ushort_t sl, char *fmt, ...)
3230Sstevel@tonic-gate {
3240Sstevel@tonic-gate 	if (sl & log_global.lz_active) {
3250Sstevel@tonic-gate 		va_list adx;
3260Sstevel@tonic-gate 		va_start(adx, fmt);
3270Sstevel@tonic-gate 		cprintf(fmt, adx, sl, "", "", caller(), mid, sid, level,
3280Sstevel@tonic-gate 		    GLOBAL_ZONEID);
3290Sstevel@tonic-gate 		va_end(adx);
3300Sstevel@tonic-gate 	}
3310Sstevel@tonic-gate 	return (1);
3320Sstevel@tonic-gate }
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate int
vstrlog(short mid,short sid,char level,ushort_t sl,char * fmt,va_list adx)3350Sstevel@tonic-gate vstrlog(short mid, short sid, char level, ushort_t sl, char *fmt, va_list adx)
3360Sstevel@tonic-gate {
3370Sstevel@tonic-gate 	if (sl & log_global.lz_active)
3380Sstevel@tonic-gate 		cprintf(fmt, adx, sl, "", "", caller(), mid, sid, level,
3390Sstevel@tonic-gate 		    GLOBAL_ZONEID);
3400Sstevel@tonic-gate 	return (1);
3410Sstevel@tonic-gate }
342