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