xref: /titanic_44/usr/src/lib/libfakekernel/common/printf.c (revision b819cea2f73f98c5662230cc9affc8cc84f77fcf)
1*b819cea2SGordon Ross /*
2*b819cea2SGordon Ross  * CDDL HEADER START
3*b819cea2SGordon Ross  *
4*b819cea2SGordon Ross  * The contents of this file are subject to the terms of the
5*b819cea2SGordon Ross  * Common Development and Distribution License (the "License").
6*b819cea2SGordon Ross  * You may not use this file except in compliance with the License.
7*b819cea2SGordon Ross  *
8*b819cea2SGordon Ross  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*b819cea2SGordon Ross  * or http://www.opensolaris.org/os/licensing.
10*b819cea2SGordon Ross  * See the License for the specific language governing permissions
11*b819cea2SGordon Ross  * and limitations under the License.
12*b819cea2SGordon Ross  *
13*b819cea2SGordon Ross  * When distributing Covered Code, include this CDDL HEADER in each
14*b819cea2SGordon Ross  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*b819cea2SGordon Ross  * If applicable, add the following below this CDDL HEADER, with the
16*b819cea2SGordon Ross  * fields enclosed by brackets "[]" replaced with your own identifying
17*b819cea2SGordon Ross  * information: Portions Copyright [yyyy] [name of copyright owner]
18*b819cea2SGordon Ross  *
19*b819cea2SGordon Ross  * CDDL HEADER END
20*b819cea2SGordon Ross  */
21*b819cea2SGordon Ross /*
22*b819cea2SGordon Ross  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23*b819cea2SGordon Ross  * Copyright (c) 2012 by Delphix. All rights reserved.
24*b819cea2SGordon Ross  * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
25*b819cea2SGordon Ross  */
26*b819cea2SGordon Ross 
27*b819cea2SGordon Ross #include <sys/param.h>
28*b819cea2SGordon Ross #include <sys/types.h>
29*b819cea2SGordon Ross #include <sys/varargs.h>
30*b819cea2SGordon Ross #include <sys/systm.h>
31*b819cea2SGordon Ross #include <sys/cmn_err.h>
32*b819cea2SGordon Ross #include <sys/log.h>
33*b819cea2SGordon Ross 
34*b819cea2SGordon Ross #include <fakekernel.h>
35*b819cea2SGordon Ross 
36*b819cea2SGordon Ross void	abort(void) __NORETURN;
37*b819cea2SGordon Ross 
38*b819cea2SGordon Ross char *volatile panicstr;
39*b819cea2SGordon Ross va_list  panicargs;
40*b819cea2SGordon Ross char panicbuf[512];
41*b819cea2SGordon Ross 
42*b819cea2SGordon Ross volatile int aok;
43*b819cea2SGordon Ross 
44*b819cea2SGordon Ross static const int
45*b819cea2SGordon Ross ce_flags[CE_IGNORE] = { SL_NOTE, SL_NOTE, SL_WARN, SL_FATAL };
46*b819cea2SGordon Ross static const char
47*b819cea2SGordon Ross ce_prefix[CE_IGNORE][10] = { "", "NOTICE: ", "WARNING: ", "" };
48*b819cea2SGordon Ross static const char
49*b819cea2SGordon Ross ce_suffix[CE_IGNORE][2] = { "", "\n", "\n", "" };
50*b819cea2SGordon Ross 
51*b819cea2SGordon Ross 
52*b819cea2SGordon Ross /*
53*b819cea2SGordon Ross  * This function is just a stub, exported NODIRECT so that
54*b819cea2SGordon Ross  * comsumers like fksmbd can provide their own.
55*b819cea2SGordon Ross  * (One that actually prints the messages.)
56*b819cea2SGordon Ross  *
57*b819cea2SGordon Ross  * It's used by fakekernel_cprintf() below.
58*b819cea2SGordon Ross  * The flags are SL_... from strlog.h
59*b819cea2SGordon Ross  */
60*b819cea2SGordon Ross /* ARGSUSED */
61*b819cea2SGordon Ross void
fakekernel_putlog(char * msg,size_t len,int flags)62*b819cea2SGordon Ross fakekernel_putlog(char *msg, size_t len, int flags)
63*b819cea2SGordon Ross {
64*b819cea2SGordon Ross }
65*b819cea2SGordon Ross 
66*b819cea2SGordon Ross /*
67*b819cea2SGordon Ross  * fakekernel_cprintf() corresponds to os/printf.c:cprintf()
68*b819cea2SGordon Ross  * This formats the message and calls fakekernel_putlog().
69*b819cea2SGordon Ross  * It's exported NODIRECT to allow replacment.
70*b819cea2SGordon Ross  * The flags are SL_... from strlog.h
71*b819cea2SGordon Ross  */
72*b819cea2SGordon Ross void
fakekernel_cprintf(const char * fmt,va_list adx,int flags,const char * prefix,const char * suffix)73*b819cea2SGordon Ross fakekernel_cprintf(const char *fmt, va_list adx, int flags,
74*b819cea2SGordon Ross 	const char *prefix, const char *suffix)
75*b819cea2SGordon Ross {
76*b819cea2SGordon Ross 	size_t bufsize = LOG_MSGSIZE;
77*b819cea2SGordon Ross 	char buf[LOG_MSGSIZE];
78*b819cea2SGordon Ross 	char *bufp = buf;
79*b819cea2SGordon Ross 	char *msgp, *bufend;
80*b819cea2SGordon Ross 	size_t len;
81*b819cea2SGordon Ross 
82*b819cea2SGordon Ross 	if (strchr("^!?", fmt[0]) != NULL) {
83*b819cea2SGordon Ross 		if (fmt[0] == '^')
84*b819cea2SGordon Ross 			flags |= SL_CONSONLY;
85*b819cea2SGordon Ross 		else if (fmt[0] == '!')
86*b819cea2SGordon Ross 			flags |= SL_LOGONLY;
87*b819cea2SGordon Ross 		fmt++;
88*b819cea2SGordon Ross 	}
89*b819cea2SGordon Ross 
90*b819cea2SGordon Ross 	bufend = bufp + bufsize;
91*b819cea2SGordon Ross 	msgp = bufp;
92*b819cea2SGordon Ross 	msgp += snprintf(msgp, bufend - msgp, "[fake_kernel] ");
93*b819cea2SGordon Ross 	msgp += snprintf(msgp, bufend - msgp, prefix);
94*b819cea2SGordon Ross 	msgp += vsnprintf(msgp, bufend - msgp, fmt, adx);
95*b819cea2SGordon Ross 	msgp += snprintf(msgp, bufend - msgp, suffix);
96*b819cea2SGordon Ross 	len = msgp - bufp;
97*b819cea2SGordon Ross 
98*b819cea2SGordon Ross 	fakekernel_putlog(bufp, len, flags);
99*b819cea2SGordon Ross }
100*b819cea2SGordon Ross 
101*b819cea2SGordon Ross /*
102*b819cea2SGordon Ross  * "User-level crash dump", if you will.
103*b819cea2SGordon Ross  */
104*b819cea2SGordon Ross void
vpanic(const char * fmt,va_list adx)105*b819cea2SGordon Ross vpanic(const char *fmt, va_list adx)
106*b819cea2SGordon Ross {
107*b819cea2SGordon Ross 	va_list tmpargs;
108*b819cea2SGordon Ross 
109*b819cea2SGordon Ross 	panicstr = (char *)fmt;
110*b819cea2SGordon Ross 	va_copy(panicargs, adx);
111*b819cea2SGordon Ross 
112*b819cea2SGordon Ross 	va_copy(tmpargs, adx);
113*b819cea2SGordon Ross 	fakekernel_cprintf(fmt, tmpargs, SL_FATAL, "fatal: ", "\n");
114*b819cea2SGordon Ross 
115*b819cea2SGordon Ross 	/* Call libc`assfail() so that mdb ::status works */
116*b819cea2SGordon Ross 	(void) vsnprintf(panicbuf, sizeof (panicbuf), fmt, adx);
117*b819cea2SGordon Ross 	assfail(panicbuf, "(panic)", 0);
118*b819cea2SGordon Ross 
119*b819cea2SGordon Ross 	abort();	/* avoid "noreturn" warnings */
120*b819cea2SGordon Ross }
121*b819cea2SGordon Ross 
122*b819cea2SGordon Ross void
panic(const char * fmt,...)123*b819cea2SGordon Ross panic(const char *fmt, ...)
124*b819cea2SGordon Ross {
125*b819cea2SGordon Ross 	va_list adx;
126*b819cea2SGordon Ross 
127*b819cea2SGordon Ross 	va_start(adx, fmt);
128*b819cea2SGordon Ross 	vpanic(fmt, adx);
129*b819cea2SGordon Ross 	va_end(adx);
130*b819cea2SGordon Ross }
131*b819cea2SGordon Ross 
132*b819cea2SGordon Ross void
vcmn_err(int ce,const char * fmt,va_list adx)133*b819cea2SGordon Ross vcmn_err(int ce, const char *fmt, va_list adx)
134*b819cea2SGordon Ross {
135*b819cea2SGordon Ross 
136*b819cea2SGordon Ross 	if (ce == CE_PANIC)
137*b819cea2SGordon Ross 		vpanic(fmt, adx);
138*b819cea2SGordon Ross 	if (ce >= CE_IGNORE)
139*b819cea2SGordon Ross 		return;
140*b819cea2SGordon Ross 
141*b819cea2SGordon Ross 	fakekernel_cprintf(fmt, adx, ce_flags[ce] | SL_CONSOLE,
142*b819cea2SGordon Ross 	    ce_prefix[ce], ce_suffix[ce]);
143*b819cea2SGordon Ross }
144*b819cea2SGordon Ross 
145*b819cea2SGordon Ross /*PRINTFLIKE2*/
146*b819cea2SGordon Ross void
cmn_err(int ce,const char * fmt,...)147*b819cea2SGordon Ross cmn_err(int ce, const char *fmt, ...)
148*b819cea2SGordon Ross {
149*b819cea2SGordon Ross 	va_list adx;
150*b819cea2SGordon Ross 
151*b819cea2SGordon Ross 	va_start(adx, fmt);
152*b819cea2SGordon Ross 	vcmn_err(ce, fmt, adx);
153*b819cea2SGordon Ross 	va_end(adx);
154*b819cea2SGordon Ross }
155