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