xref: /titanic_51/usr/src/uts/common/os/printf.c (revision eaaa4d5f5eed6a67a490a59a60ac9bacbd3ca78b)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  *
25  * Copyright 2012 Nexenta Systems, Inc.  All rights reserved.
26  */
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 #include <sys/sunddi.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 *sp;
146 
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);
152 			tty_rele(sp);
153 		}
154 	}
155 
156 	if (on_intr && !panicstr) {
157 		(void) putq(log_intrq, mp);
158 		softcall((void (*)(void *))log_flushq, log_intrq);
159 	} else {
160 		log_sendmsg(mp, zoneid);
161 	}
162 out:
163 	if (panicbuf_log + len < PANICBUFSIZE) {
164 		uint32_t old, new;
165 		do {
166 			old = panicbuf_index;
167 			new = old + len;
168 			if (new >= PANICBUFSIZE)
169 				new = panicbuf_log + len;
170 		} while (cas32(&panicbuf_index, old, new) != old);
171 		bcopy(body, &panicbuf[new - len], len);
172 	}
173 	if (bufp != buf)
174 		kmem_free(bufp, bufsize);
175 }
176 
177 void
178 vzprintf(zoneid_t zoneid, const char *fmt, va_list adx)
179 {
180 	cprintf(fmt, adx, SL_CONSOLE | SL_NOTE, "", "", caller(), 0, 0, 0,
181 	    zoneid);
182 }
183 
184 void
185 vprintf(const char *fmt, va_list adx)
186 {
187 	vzprintf(GLOBAL_ZONEID, fmt, adx);
188 }
189 
190 /*PRINTFLIKE1*/
191 void
192 printf(const char *fmt, ...)
193 {
194 	va_list adx;
195 
196 	va_start(adx, fmt);
197 	cprintf(fmt, adx, SL_CONSOLE | SL_NOTE, "", "", caller(), 0, 0, 0,
198 	    GLOBAL_ZONEID);
199 	va_end(adx);
200 }
201 
202 /*PRINTFLIKE2*/
203 void
204 zprintf(zoneid_t zoneid, const char *fmt, ...)
205 {
206 	va_list adx;
207 
208 	va_start(adx, fmt);
209 	cprintf(fmt, adx, SL_CONSOLE | SL_NOTE, "", "", caller(), 0, 0, 0,
210 	    zoneid);
211 	va_end(adx);
212 }
213 
214 void
215 vuprintf(const char *fmt, va_list adx)
216 {
217 	va_list adxcp;
218 	va_copy(adxcp, adx);
219 
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);
223 
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());
228 
229 	va_end(adxcp);
230 }
231 
232 /*PRINTFLIKE1*/
233 void
234 uprintf(const char *fmt, ...)
235 {
236 	va_list adx;
237 
238 	va_start(adx, fmt);
239 
240 	vuprintf(fmt, adx);
241 
242 	va_end(adx);
243 }
244 
245 void
246 vzcmn_err(zoneid_t zoneid, int ce, const char *fmt, va_list adx)
247 {
248 	if (ce == CE_PANIC)
249 		vpanic(fmt, 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,
253 		    zoneid);
254 }
255 
256 void
257 vcmn_err(int ce, const char *fmt, va_list adx)
258 {
259 	vzcmn_err(GLOBAL_ZONEID, ce, fmt, adx);
260 }
261 
262 /*PRINTFLIKE2*/
263 void
264 cmn_err(int ce, const char *fmt, ...)
265 {
266 	va_list adx;
267 
268 	va_start(adx, fmt);
269 	if (ce == CE_PANIC)
270 		vpanic(fmt, adx);
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,
274 		    GLOBAL_ZONEID);
275 	va_end(adx);
276 }
277 
278 /*PRINTFLIKE3*/
279 void
280 zcmn_err(zoneid_t zoneid, int ce, const char *fmt, ...)
281 {
282 	va_list adx;
283 
284 	va_start(adx, fmt);
285 	if (ce == CE_PANIC)
286 		vpanic(fmt, adx);
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);
290 	va_end(adx);
291 }
292 
293 /*PRINTFLIKE3*/
294 void
295 dev_err(dev_info_t *dip, int ce, char *fmt, ...)
296 {
297 	va_list ap;
298 	char buf[LOG_MSGSIZE];
299 
300 	(void) snprintf(buf, sizeof (buf), "%s%d: %s",
301 	    ddi_driver_name(dip), ddi_get_instance(dip), fmt);
302 
303 	va_start(ap, fmt);
304 	vcmn_err(ce, buf, ap);
305 	va_end(ap);
306 }
307 
308 int
309 assfail(const char *a, const char *f, int l)
310 {
311 	if (aask)  {
312 		printf("ASSERTION CAUGHT: %s, file: %s, line: %d", a, f, l);
313 		debug_enter(NULL);
314 	}
315 
316 	if (!aok && !panicstr)
317 		panic("assertion failed: %s, file: %s, line: %d", a, f, l);
318 
319 	return (0);
320 }
321 
322 void
323 assfail3(const char *a, uintmax_t lv, const char *op, uintmax_t rv,
324     const char *f, int l)
325 {
326 	if (aask)  {
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,
329 		    f, l);
330 		debug_enter(NULL);
331 	}
332 
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,
336 		    f, l);
337 }
338 
339 int
340 strlog(short mid, short sid, char level, ushort_t sl, char *fmt, ...)
341 {
342 	if (sl & log_global.lz_active) {
343 		va_list adx;
344 		va_start(adx, fmt);
345 		cprintf(fmt, adx, sl, "", "", caller(), mid, sid, level,
346 		    GLOBAL_ZONEID);
347 		va_end(adx);
348 	}
349 	return (1);
350 }
351 
352 int
353 vstrlog(short mid, short sid, char level, ushort_t sl, char *fmt, va_list adx)
354 {
355 	if (sl & log_global.lz_active)
356 		cprintf(fmt, adx, sl, "", "", caller(), mid, sid, level,
357 		    GLOBAL_ZONEID);
358 	return (1);
359 }
360