xref: /titanic_41/usr/src/uts/common/os/printf.c (revision 1f09d37cbb915965c40754f238d96e861a30213e)
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 2014 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, dev_info_t *dip)
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 	if (dip != NULL)
116 		msgp += snprintf(msgp, bufend - msgp, "%s%d: ",
117 		    ddi_driver_name(dip), ddi_get_instance(dip));
118 	msgp += vsnprintf(msgp, bufend - msgp, fmt, adx);
119 	msgp += snprintf(msgp, bufend - msgp, suffix);
120 	len = strlen(body);
121 
122 	if (((sl & SL_CONSONLY) && panicstr) ||
123 	    (zoneid == GLOBAL_ZONEID && log_global.lz_active == 0)) {
124 		console_printf("%s", body);
125 		goto out;
126 	}
127 
128 	if (msgp - bufp >= bufsize && !on_intr) {
129 		ASSERT(bufp == buf);
130 		bufsize = msgp - bufp + 1;
131 		bufp = kmem_alloc(bufsize, KM_NOSLEEP);
132 		if (bufp != NULL)
133 			goto retry;
134 		bufsize = LOG_MSGSIZE;
135 		bufp = buf;
136 	}
137 
138 	mp = log_makemsg(mid, sid, level, sl, LOG_KERN, bufp,
139 	    MIN(bufsize, msgp - bufp + 1), on_intr);
140 	if (mp == NULL) {
141 		if ((sl & (SL_CONSOLE | SL_LOGONLY)) == SL_CONSOLE && !on_intr)
142 			console_printf("%s", body);
143 		goto out;
144 	}
145 
146 	if (sl & SL_USER) {
147 		ssize_t resid;
148 		sess_t *sp;
149 
150 		if ((sp = tty_hold()) != NULL) {
151 			if (sp->s_vp != NULL)
152 				(void) vn_rdwr(UIO_WRITE, sp->s_vp, body,
153 				    len, 0LL, UIO_SYSSPACE, FAPPEND,
154 				    (rlim64_t)LOG_HIWAT, kcred, &resid);
155 			tty_rele(sp);
156 		}
157 	}
158 
159 	if (on_intr && !panicstr) {
160 		(void) putq(log_intrq, mp);
161 		softcall((void (*)(void *))log_flushq, log_intrq);
162 	} else {
163 		log_sendmsg(mp, zoneid);
164 	}
165 out:
166 	if (panicbuf_log + len < PANICBUFSIZE) {
167 		uint32_t old, new;
168 		do {
169 			old = panicbuf_index;
170 			new = old + len;
171 			if (new >= PANICBUFSIZE)
172 				new = panicbuf_log + len;
173 		} while (atomic_cas_32(&panicbuf_index, old, new) != old);
174 		bcopy(body, &panicbuf[new - len], len);
175 	}
176 	if (bufp != buf)
177 		kmem_free(bufp, bufsize);
178 }
179 
180 void
181 vzprintf(zoneid_t zoneid, const char *fmt, va_list adx)
182 {
183 	cprintf(fmt, adx, SL_CONSOLE | SL_NOTE, "", "", caller(), 0, 0, 0,
184 	    zoneid, NULL);
185 }
186 
187 void
188 vprintf(const char *fmt, va_list adx)
189 {
190 	vzprintf(GLOBAL_ZONEID, fmt, adx);
191 }
192 
193 /*PRINTFLIKE1*/
194 void
195 printf(const char *fmt, ...)
196 {
197 	va_list adx;
198 
199 	va_start(adx, fmt);
200 	cprintf(fmt, adx, SL_CONSOLE | SL_NOTE, "", "", caller(), 0, 0, 0,
201 	    GLOBAL_ZONEID, NULL);
202 	va_end(adx);
203 }
204 
205 /*PRINTFLIKE2*/
206 void
207 zprintf(zoneid_t zoneid, const char *fmt, ...)
208 {
209 	va_list adx;
210 
211 	va_start(adx, fmt);
212 	cprintf(fmt, adx, SL_CONSOLE | SL_NOTE, "", "", caller(), 0, 0, 0,
213 	    zoneid, NULL);
214 	va_end(adx);
215 }
216 
217 void
218 vuprintf(const char *fmt, va_list adx)
219 {
220 	va_list adxcp;
221 	va_copy(adxcp, adx);
222 
223 	/* Message the user tty, if any, and the global zone syslog */
224 	cprintf(fmt, adx, SL_CONSOLE | SL_LOGONLY | SL_USER | SL_NOTE,
225 	    "", "", caller(), 0, 0, 0, GLOBAL_ZONEID, NULL);
226 
227 	/* Now message the local zone syslog */
228 	if (!INGLOBALZONE(curproc))
229 		cprintf(fmt, adxcp, SL_CONSOLE | SL_LOGONLY | SL_NOTE,
230 		    "", "", caller(), 0, 0, 0, getzoneid(), NULL);
231 
232 	va_end(adxcp);
233 }
234 
235 /*PRINTFLIKE1*/
236 void
237 uprintf(const char *fmt, ...)
238 {
239 	va_list adx;
240 
241 	va_start(adx, fmt);
242 
243 	vuprintf(fmt, adx);
244 
245 	va_end(adx);
246 }
247 
248 static void
249 vzdcmn_err(zoneid_t zoneid, int ce, const char *fmt, va_list adx,
250     dev_info_t *dip)
251 {
252 	if (ce == CE_PANIC)
253 		vpanic(fmt, adx);
254 	if ((uint_t)ce < CE_IGNORE)
255 		cprintf(fmt, adx, ce_to_sl[ce] | SL_CONSOLE,
256 		    ce_prefix[ce], ce_suffix[ce], caller(), 0, 0, 0,
257 		    zoneid, dip);
258 }
259 
260 void
261 vzcmn_err(zoneid_t zoneid, int ce, const char *fmt, va_list adx)
262 {
263 	vzdcmn_err(zoneid, ce, fmt, adx, NULL);
264 }
265 
266 void
267 vcmn_err(int ce, const char *fmt, va_list adx)
268 {
269 	vzdcmn_err(GLOBAL_ZONEID, ce, fmt, adx, NULL);
270 }
271 
272 /*PRINTFLIKE2*/
273 void
274 cmn_err(int ce, const char *fmt, ...)
275 {
276 	va_list adx;
277 
278 	va_start(adx, fmt);
279 	vzdcmn_err(GLOBAL_ZONEID, ce, fmt, adx, NULL);
280 	va_end(adx);
281 }
282 
283 /*PRINTFLIKE3*/
284 void
285 zcmn_err(zoneid_t zoneid, int ce, const char *fmt, ...)
286 {
287 	va_list adx;
288 
289 	va_start(adx, fmt);
290 	vzdcmn_err(zoneid, ce, fmt, adx, NULL);
291 	va_end(adx);
292 }
293 
294 /*PRINTFLIKE3*/
295 void
296 dev_err(dev_info_t *dip, int ce, char *fmt, ...)
297 {
298 	va_list adx;
299 
300 	va_start(adx, fmt);
301 	vzdcmn_err(GLOBAL_ZONEID, ce, fmt, adx, dip);
302 	va_end(adx);
303 }
304 
305 int
306 assfail(const char *a, const char *f, int l)
307 {
308 	if (aask)  {
309 		printf("ASSERTION CAUGHT: %s, file: %s, line: %d", a, f, l);
310 		debug_enter(NULL);
311 	}
312 
313 	if (!aok && !panicstr)
314 		panic("assertion failed: %s, file: %s, line: %d", a, f, l);
315 
316 	return (0);
317 }
318 
319 void
320 assfail3(const char *a, uintmax_t lv, const char *op, uintmax_t rv,
321     const char *f, int l)
322 {
323 	if (aask)  {
324 		printf("ASSERTION CAUGHT: %s (0x%llx %s 0x%llx), file: %s, "
325 		    "line: %d", a, (u_longlong_t)lv, op, (u_longlong_t)rv,
326 		    f, l);
327 		debug_enter(NULL);
328 	}
329 
330 	if (!aok && !panicstr)
331 		panic("assertion failed: %s (0x%llx %s 0x%llx), file: %s, "
332 		    "line: %d", a, (u_longlong_t)lv, op, (u_longlong_t)rv,
333 		    f, l);
334 }
335 
336 int
337 strlog(short mid, short sid, char level, ushort_t sl, char *fmt, ...)
338 {
339 	if (sl & log_global.lz_active) {
340 		va_list adx;
341 		va_start(adx, fmt);
342 		cprintf(fmt, adx, sl, "", "", caller(), mid, sid, level,
343 		    GLOBAL_ZONEID, NULL);
344 		va_end(adx);
345 	}
346 	return (1);
347 }
348 
349 int
350 vstrlog(short mid, short sid, char level, ushort_t sl, char *fmt, va_list adx)
351 {
352 	if (sl & log_global.lz_active)
353 		cprintf(fmt, adx, sl, "", "", caller(), mid, sid, level,
354 		    GLOBAL_ZONEID, NULL);
355 	return (1);
356 }
357