xref: /titanic_50/usr/src/uts/common/os/printf.c (revision 2a8d6eba033e4713ab12b61178f0513f1f075482)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
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>
34 #include <sys/conf.h>
35 #include <sys/cmn_err.h>
36 #include <sys/syslog.h>
37 #include <sys/log.h>
38 #include <sys/proc.h>
39 #include <sys/vnode.h>
40 #include <sys/session.h>
41 #include <sys/stream.h>
42 #include <sys/kmem.h>
43 #include <sys/kobj.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>
51 #include <sys/spl.h>
52 #include <sys/zone.h>
53 
54 /*
55  * In some debugging situations it's useful to log all messages to panicbuf,
56  * which is persistent across reboots (on most platforms).  The range
57  * panicbuf[panicbuf_log..PANICBUFSIZE-1] may be used for this purpose.
58  * By default, panicbuf_log == PANICBUFSIZE and no messages are logged.
59  * To enable panicbuf logging, set panicbuf_log to a small value, say 1K;
60  * this will reserve 1K for panic information and 7K for message logging.
61  */
62 uint32_t panicbuf_log = PANICBUFSIZE;
63 uint32_t panicbuf_index = PANICBUFSIZE;
64 
65 static int aask, aok;
66 static int ce_to_sl[CE_IGNORE] = { SL_NOTE, SL_NOTE, SL_WARN, SL_FATAL };
67 static char ce_prefix[CE_IGNORE][10] = { "", "NOTICE: ", "WARNING: ", "" };
68 static char ce_suffix[CE_IGNORE][2] = { "", "\n", "\n", "" };
69 
70 static void
71 cprintf(const char *fmt, va_list adx, int sl, const char *prefix,
72 	const char *suffix, void *site, int mid, int sid, int level,
73 	zoneid_t zoneid)
74 {
75 	uint32_t msgid;
76 	size_t bufsize = LOG_MSGSIZE;
77 	char buf[LOG_MSGSIZE];
78 	char *bufp = buf;
79 	char *body, *msgp, *bufend;
80 	mblk_t *mp;
81 	int s, on_intr;
82 	size_t len;
83 
84 	s = splhi();
85 	on_intr = CPU_ON_INTR(CPU) ||
86 	    (interrupts_unleashed && (spltoipl(s) > LOCK_LEVEL));
87 	splx(s);
88 
89 	ASSERT(zoneid == GLOBAL_ZONEID || !on_intr);
90 
91 	STRLOG_MAKE_MSGID(fmt, msgid);
92 
93 	if (strchr("^!?", fmt[0]) != NULL) {
94 		if (fmt[0] == '^')
95 			sl |= SL_CONSONLY;
96 		else if (fmt[0] == '!' ||
97 		    (prefix[0] == '\0' && !(boothowto & RB_VERBOSE)))
98 			sl = (sl & ~(SL_USER | SL_NOTE)) | SL_LOGONLY;
99 		fmt++;
100 	}
101 
102 	if ((sl & SL_USER) && (MUTEX_HELD(&pidlock) || on_intr)) {
103 		zoneid = getzoneid();
104 		sl = sl & ~(SL_USER | SL_LOGONLY) | SL_CONSOLE;
105 	}
106 
107 retry:
108 	bufend = bufp + bufsize;
109 	msgp = bufp;
110 	body = msgp += snprintf(msgp, bufend - msgp,
111 	    "%s: [ID %u FACILITY_AND_PRIORITY] ",
112 	    mod_containing_pc(site), msgid);
113 	msgp += snprintf(msgp, bufend - msgp, prefix);
114 	msgp += vsnprintf(msgp, bufend - msgp, fmt, adx);
115 	msgp += snprintf(msgp, bufend - msgp, suffix);
116 	len = strlen(body);
117 
118 	if (((sl & SL_CONSONLY) && panicstr) ||
119 	    (zoneid == GLOBAL_ZONEID && log_global.lz_active == 0)) {
120 		console_printf("%s", body);
121 		goto out;
122 	}
123 
124 	if (msgp - bufp >= bufsize && !on_intr) {
125 		ASSERT(bufp == buf);
126 		bufsize = msgp - bufp + 1;
127 		bufp = kmem_alloc(bufsize, KM_NOSLEEP);
128 		if (bufp != NULL)
129 			goto retry;
130 		bufsize = LOG_MSGSIZE;
131 		bufp = buf;
132 	}
133 
134 	mp = log_makemsg(mid, sid, level, sl, LOG_KERN, bufp,
135 	    MIN(bufsize, msgp - bufp + 1), on_intr);
136 	if (mp == NULL) {
137 		if ((sl & (SL_CONSOLE | SL_LOGONLY)) == SL_CONSOLE && !on_intr)
138 			console_printf("%s", body);
139 		goto out;
140 	}
141 
142 	if (sl & SL_USER) {
143 		ssize_t resid;
144 		sess_t *sp;
145 
146 		if ((sp = tty_hold()) != NULL) {
147 			if (sp->s_vp != NULL)
148 				(void) vn_rdwr(UIO_WRITE, sp->s_vp, body,
149 				    len, 0LL, UIO_SYSSPACE, FAPPEND,
150 				    (rlim64_t)LOG_HIWAT, kcred, &resid);
151 			tty_rele(sp);
152 		}
153 	}
154 
155 	if (on_intr && !panicstr) {
156 		(void) putq(log_intrq, mp);
157 		softcall((void (*)(void *))log_flushq, log_intrq);
158 	} else {
159 		log_sendmsg(mp, zoneid);
160 	}
161 out:
162 	if (panicbuf_log + len < PANICBUFSIZE) {
163 		uint32_t old, new;
164 		do {
165 			old = panicbuf_index;
166 			new = old + len;
167 			if (new >= PANICBUFSIZE)
168 				new = panicbuf_log + len;
169 		} while (cas32(&panicbuf_index, old, new) != old);
170 		bcopy(body, &panicbuf[new - len], len);
171 	}
172 	if (bufp != buf)
173 		kmem_free(bufp, bufsize);
174 }
175 
176 void
177 vzprintf(zoneid_t zoneid, const char *fmt, va_list adx)
178 {
179 	cprintf(fmt, adx, SL_CONSOLE | SL_NOTE, "", "", caller(), 0, 0, 0,
180 	    zoneid);
181 }
182 
183 void
184 vprintf(const char *fmt, va_list adx)
185 {
186 	vzprintf(GLOBAL_ZONEID, fmt, adx);
187 }
188 
189 /*PRINTFLIKE1*/
190 void
191 printf(const char *fmt, ...)
192 {
193 	va_list adx;
194 
195 	va_start(adx, fmt);
196 	cprintf(fmt, adx, SL_CONSOLE | SL_NOTE, "", "", caller(), 0, 0, 0,
197 	    GLOBAL_ZONEID);
198 	va_end(adx);
199 }
200 
201 /*PRINTFLIKE2*/
202 void
203 zprintf(zoneid_t zoneid, const char *fmt, ...)
204 {
205 	va_list adx;
206 
207 	va_start(adx, fmt);
208 	cprintf(fmt, adx, SL_CONSOLE | SL_NOTE, "", "", caller(), 0, 0, 0,
209 	    zoneid);
210 	va_end(adx);
211 }
212 
213 void
214 vuprintf(const char *fmt, va_list adx)
215 {
216 	cprintf(fmt, adx, SL_CONSOLE | SL_LOGONLY | SL_USER | SL_NOTE,
217 	    "", "", caller(), 0, 0, 0, GLOBAL_ZONEID);
218 }
219 
220 /*PRINTFLIKE1*/
221 void
222 uprintf(const char *fmt, ...)
223 {
224 	va_list adx;
225 
226 	va_start(adx, fmt);
227 	cprintf(fmt, adx, SL_CONSOLE | SL_LOGONLY | SL_USER | SL_NOTE,
228 	    "", "", caller(), 0, 0, 0, GLOBAL_ZONEID);
229 	va_end(adx);
230 }
231 
232 void
233 vzcmn_err(zoneid_t zoneid, int ce, const char *fmt, va_list adx)
234 {
235 	if (ce == CE_PANIC)
236 		vpanic(fmt, adx);
237 	if ((uint_t)ce < CE_IGNORE)
238 		cprintf(fmt, adx, ce_to_sl[ce] | SL_CONSOLE,
239 		    ce_prefix[ce], ce_suffix[ce], caller(), 0, 0, 0,
240 		    zoneid);
241 }
242 
243 void
244 vcmn_err(int ce, const char *fmt, va_list adx)
245 {
246 	vzcmn_err(GLOBAL_ZONEID, ce, fmt, adx);
247 }
248 
249 /*PRINTFLIKE2*/
250 void
251 cmn_err(int ce, const char *fmt, ...)
252 {
253 	va_list adx;
254 
255 	va_start(adx, fmt);
256 	if (ce == CE_PANIC)
257 		vpanic(fmt, adx);
258 	if ((uint_t)ce < CE_IGNORE)
259 		cprintf(fmt, adx, ce_to_sl[ce] | SL_CONSOLE,
260 		    ce_prefix[ce], ce_suffix[ce], caller(), 0, 0, 0,
261 		    GLOBAL_ZONEID);
262 	va_end(adx);
263 }
264 
265 /*PRINTFLIKE3*/
266 void
267 zcmn_err(zoneid_t zoneid, int ce, const char *fmt, ...)
268 {
269 	va_list adx;
270 
271 	va_start(adx, fmt);
272 	if (ce == CE_PANIC)
273 		vpanic(fmt, adx);
274 	if ((uint_t)ce < CE_IGNORE)
275 		cprintf(fmt, adx, ce_to_sl[ce] | SL_CONSOLE, ce_prefix[ce],
276 		    ce_suffix[ce], caller(), 0, 0, 0, zoneid);
277 	va_end(adx);
278 }
279 
280 int
281 assfail(const char *a, const char *f, int l)
282 {
283 	if (aask)  {
284 		printf("ASSERTION CAUGHT: %s, file: %s, line: %d", a, f, l);
285 		debug_enter(NULL);
286 	}
287 
288 	if (!aok && !panicstr)
289 		panic("assertion failed: %s, file: %s, line: %d", a, f, l);
290 
291 	return (0);
292 }
293 
294 void
295 assfail3(const char *a, uintmax_t lv, const char *op, uintmax_t rv,
296     const char *f, int l)
297 {
298 	if (aask)  {
299 		printf("ASSERTION CAUGHT: %s (0x%llx %s 0x%llx), file: %s, "
300 		    "line: %d", a, (u_longlong_t)lv, op, (u_longlong_t)rv,
301 		    f, l);
302 		debug_enter(NULL);
303 	}
304 
305 	if (!aok && !panicstr)
306 		panic("assertion failed: %s (0x%llx %s 0x%llx), file: %s, "
307 		    "line: %d", a, (u_longlong_t)lv, op, (u_longlong_t)rv,
308 		    f, l);
309 }
310 
311 int
312 strlog(short mid, short sid, char level, ushort_t sl, char *fmt, ...)
313 {
314 	if (sl & log_global.lz_active) {
315 		va_list adx;
316 		va_start(adx, fmt);
317 		cprintf(fmt, adx, sl, "", "", caller(), mid, sid, level,
318 		    GLOBAL_ZONEID);
319 		va_end(adx);
320 	}
321 	return (1);
322 }
323 
324 int
325 vstrlog(short mid, short sid, char level, ushort_t sl, char *fmt, va_list adx)
326 {
327 	if (sl & log_global.lz_active)
328 		cprintf(fmt, adx, sl, "", "", caller(), mid, sid, level,
329 		    GLOBAL_ZONEID);
330 	return (1);
331 }
332