xref: /titanic_41/usr/src/uts/common/os/printf.c (revision 445f2479fe3d7435daab18bf2cdc310b86cd6738)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/param.h>
30 #include <sys/types.h>
31 #include <sys/sysmacros.h>
32 #include <sys/inline.h>
33 #include <sys/varargs.h>
34 #include <sys/systm.h>
35 #include <sys/conf.h>
36 #include <sys/cmn_err.h>
37 #include <sys/syslog.h>
38 #include <sys/log.h>
39 #include <sys/proc.h>
40 #include <sys/vnode.h>
41 #include <sys/session.h>
42 #include <sys/stream.h>
43 #include <sys/kmem.h>
44 #include <sys/kobj.h>
45 #include <sys/atomic.h>
46 #include <sys/console.h>
47 #include <sys/cpuvar.h>
48 #include <sys/modctl.h>
49 #include <sys/reboot.h>
50 #include <sys/debug.h>
51 #include <sys/panic.h>
52 #include <sys/spl.h>
53 #include <sys/zone.h>
54 
55 /*
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.
62  */
63 uint32_t panicbuf_log = PANICBUFSIZE;
64 uint32_t panicbuf_index = PANICBUFSIZE;
65 
66 static int aask, aok;
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", "" };
70 
71 static void
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,
74 	zoneid_t zoneid)
75 {
76 	uint32_t msgid;
77 	size_t bufsize = LOG_MSGSIZE;
78 	char buf[LOG_MSGSIZE];
79 	char *bufp = buf;
80 	char *body, *msgp, *bufend;
81 	mblk_t *mp;
82 	int s, on_intr;
83 	size_t len;
84 
85 	s = splhi();
86 	on_intr = CPU_ON_INTR(CPU) ||
87 	    (interrupts_unleashed && (spltoipl(s) > LOCK_LEVEL));
88 	splx(s);
89 
90 	ASSERT(zoneid == GLOBAL_ZONEID || !on_intr);
91 
92 	STRLOG_MAKE_MSGID(fmt, msgid);
93 
94 	if (strchr("^!?", fmt[0]) != NULL) {
95 		if (fmt[0] == '^')
96 			sl |= SL_CONSONLY;
97 		else if (fmt[0] == '!' ||
98 		    (prefix[0] == '\0' && !(boothowto & RB_VERBOSE)))
99 			sl = (sl & ~(SL_USER | SL_NOTE)) | SL_LOGONLY;
100 		fmt++;
101 	}
102 
103 	if ((sl & SL_USER) && (MUTEX_HELD(&pidlock) || on_intr)) {
104 		zoneid = getzoneid();
105 		sl = sl & ~(SL_USER | SL_LOGONLY) | SL_CONSOLE;
106 	}
107 
108 retry:
109 	bufend = bufp + bufsize;
110 	msgp = bufp;
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);
117 	len = strlen(body);
118 
119 	if (((sl & SL_CONSONLY) && panicstr) ||
120 	    (zoneid == GLOBAL_ZONEID && log_global.lz_active == 0)) {
121 		console_printf("%s", body);
122 		goto out;
123 	}
124 
125 	if (msgp - bufp >= bufsize && !on_intr) {
126 		ASSERT(bufp == buf);
127 		bufsize = msgp - bufp + 1;
128 		bufp = kmem_alloc(bufsize, KM_NOSLEEP);
129 		if (bufp != NULL)
130 			goto retry;
131 		bufsize = LOG_MSGSIZE;
132 		bufp = buf;
133 	}
134 
135 	mp = log_makemsg(mid, sid, level, sl, LOG_KERN, bufp,
136 	    MIN(bufsize, msgp - bufp + 1), on_intr);
137 	if (mp == NULL) {
138 		if ((sl & (SL_CONSOLE | SL_LOGONLY)) == SL_CONSOLE && !on_intr)
139 			console_printf("%s", body);
140 		goto out;
141 	}
142 
143 	if (sl & SL_USER) {
144 		ssize_t resid;
145 		sess_t *sessp;
146 
147 		mutex_enter(&pidlock);
148 		sessp = curproc->p_sessp;
149 		SESS_HOLD(sessp);
150 		TTY_HOLD(sessp);
151 		mutex_exit(&pidlock);
152 		if (sessp->s_vp)
153 			(void) vn_rdwr(UIO_WRITE, sessp->s_vp,
154 			    body, len, 0LL, UIO_SYSSPACE,
155 			    FAPPEND, (rlim64_t)LOG_HIWAT, kcred, &resid);
156 		mutex_enter(&pidlock);
157 		TTY_RELE(sessp);
158 		SESS_RELE(sessp);
159 		mutex_exit(&pidlock);
160 	}
161 
162 	if (on_intr && !panicstr) {
163 		(void) putq(log_intrq, mp);
164 		softcall((void (*)(void *))log_flushq, log_intrq);
165 	} else {
166 		log_sendmsg(mp, zoneid);
167 	}
168 out:
169 	if (panicbuf_log + len < PANICBUFSIZE) {
170 		uint32_t old, new;
171 		do {
172 			old = panicbuf_index;
173 			new = old + len;
174 			if (new >= PANICBUFSIZE)
175 				new = panicbuf_log + len;
176 		} while (cas32(&panicbuf_index, old, new) != old);
177 		bcopy(body, &panicbuf[new - len], len);
178 	}
179 	if (bufp != buf)
180 		kmem_free(bufp, bufsize);
181 }
182 
183 void
184 vzprintf(zoneid_t zoneid, const char *fmt, va_list adx)
185 {
186 	cprintf(fmt, adx, SL_CONSOLE | SL_NOTE, "", "", caller(), 0, 0, 0,
187 	    zoneid);
188 }
189 
190 void
191 vprintf(const char *fmt, va_list adx)
192 {
193 	vzprintf(GLOBAL_ZONEID, fmt, adx);
194 }
195 
196 /*PRINTFLIKE1*/
197 void
198 printf(const char *fmt, ...)
199 {
200 	va_list adx;
201 
202 	va_start(adx, fmt);
203 	cprintf(fmt, adx, SL_CONSOLE | SL_NOTE, "", "", caller(), 0, 0, 0,
204 	    GLOBAL_ZONEID);
205 	va_end(adx);
206 }
207 
208 /*PRINTFLIKE2*/
209 void
210 zprintf(zoneid_t zoneid, const char *fmt, ...)
211 {
212 	va_list adx;
213 
214 	va_start(adx, fmt);
215 	cprintf(fmt, adx, SL_CONSOLE | SL_NOTE, "", "", caller(), 0, 0, 0,
216 	    zoneid);
217 	va_end(adx);
218 }
219 
220 void
221 vuprintf(const char *fmt, va_list adx)
222 {
223 	cprintf(fmt, adx, SL_CONSOLE | SL_LOGONLY | SL_USER | SL_NOTE,
224 	    "", "", caller(), 0, 0, 0, GLOBAL_ZONEID);
225 }
226 
227 /*PRINTFLIKE1*/
228 void
229 uprintf(const char *fmt, ...)
230 {
231 	va_list adx;
232 
233 	va_start(adx, fmt);
234 	cprintf(fmt, adx, SL_CONSOLE | SL_LOGONLY | SL_USER | SL_NOTE,
235 	    "", "", caller(), 0, 0, 0, GLOBAL_ZONEID);
236 	va_end(adx);
237 }
238 
239 void
240 vzcmn_err(zoneid_t zoneid, int ce, const char *fmt, va_list adx)
241 {
242 	if (ce == CE_PANIC)
243 		vpanic(fmt, adx);
244 	if ((uint_t)ce < CE_IGNORE)
245 		cprintf(fmt, adx, ce_to_sl[ce] | SL_CONSOLE,
246 		    ce_prefix[ce], ce_suffix[ce], caller(), 0, 0, 0,
247 		    zoneid);
248 }
249 
250 void
251 vcmn_err(int ce, const char *fmt, va_list adx)
252 {
253 	vzcmn_err(GLOBAL_ZONEID, ce, fmt, adx);
254 }
255 
256 /*PRINTFLIKE2*/
257 void
258 cmn_err(int ce, const char *fmt, ...)
259 {
260 	va_list adx;
261 
262 	va_start(adx, fmt);
263 	if (ce == CE_PANIC)
264 		vpanic(fmt, adx);
265 	if ((uint_t)ce < CE_IGNORE)
266 		cprintf(fmt, adx, ce_to_sl[ce] | SL_CONSOLE,
267 		    ce_prefix[ce], ce_suffix[ce], caller(), 0, 0, 0,
268 		    GLOBAL_ZONEID);
269 	va_end(adx);
270 }
271 
272 /*PRINTFLIKE3*/
273 void
274 zcmn_err(zoneid_t zoneid, int ce, const char *fmt, ...)
275 {
276 	va_list adx;
277 
278 	va_start(adx, fmt);
279 	if (ce == CE_PANIC)
280 		vpanic(fmt, adx);
281 	if ((uint_t)ce < CE_IGNORE)
282 		cprintf(fmt, adx, ce_to_sl[ce] | SL_CONSOLE, ce_prefix[ce],
283 		    ce_suffix[ce], caller(), 0, 0, 0, zoneid);
284 	va_end(adx);
285 }
286 
287 int
288 assfail(const char *a, const char *f, int l)
289 {
290 	if (aask)  {
291 		printf("ASSERTION CAUGHT: %s, file: %s, line: %d", a, f, l);
292 		debug_enter(NULL);
293 	}
294 
295 	if (!aok && !panicstr)
296 		panic("assertion failed: %s, file: %s, line: %d", a, f, l);
297 
298 	return (0);
299 }
300 
301 void
302 assfail3(const char *a, uintmax_t lv, const char *op, uintmax_t rv,
303     const char *f, int l)
304 {
305 	if (aask)  {
306 		printf("ASSERTION CAUGHT: %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 		debug_enter(NULL);
310 	}
311 
312 	if (!aok && !panicstr)
313 		panic("assertion failed: %s (0x%llx %s 0x%llx), file: %s, "
314 		    "line: %d", a, (u_longlong_t)lv, op, (u_longlong_t)rv,
315 		    f, l);
316 }
317 
318 int
319 strlog(short mid, short sid, char level, ushort_t sl, char *fmt, ...)
320 {
321 	if (sl & log_global.lz_active) {
322 		va_list adx;
323 		va_start(adx, fmt);
324 		cprintf(fmt, adx, sl, "", "", caller(), mid, sid, level,
325 		    GLOBAL_ZONEID);
326 		va_end(adx);
327 	}
328 	return (1);
329 }
330 
331 int
332 vstrlog(short mid, short sid, char level, ushort_t sl, char *fmt, va_list adx)
333 {
334 	if (sl & log_global.lz_active)
335 		cprintf(fmt, adx, sl, "", "", caller(), mid, sid, level,
336 		    GLOBAL_ZONEID);
337 	return (1);
338 }
339